csgetval.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ndb.h>
  5. #include <ndbhf.h>
  6. /*
  7. * search for a tuple that has the given 'attr=val' and also 'rattr=x'.
  8. * copy 'x' into 'buf' and return the whole tuple.
  9. *
  10. * return 0 if not found.
  11. */
  12. Ndbtuple*
  13. csgetval(char *attr, char *val, char *rattr, char *buf)
  14. {
  15. Ndbtuple *t, *first, *last;
  16. int n, fd, linefound;
  17. char line[1024];
  18. buf[0] = 0;
  19. fd = open("/net/cs", ORDWR);
  20. if(fd < 0)
  21. return 0;
  22. fprint(fd, "!%s=%s", attr, val);
  23. seek(fd, 0, 0);
  24. first = last = 0;
  25. linefound = 0;
  26. for(;;){
  27. n = read(fd, line, sizeof(line)-2);
  28. if(n <= 0)
  29. break;
  30. line[n] = '\n';
  31. line[n+1] = 0;
  32. t = _ndbparseline(line);
  33. if(t == 0)
  34. continue;
  35. if(first)
  36. last->entry = t;
  37. else
  38. first = t;
  39. last = t;
  40. while(last->entry)
  41. last = last->entry;
  42. for(; t; t = t->entry){
  43. if(buf[0] == 0 || linefound == 0)
  44. if(strcmp(rattr, t->attr) == 0)
  45. strcpy(buf, t->val);
  46. if(linefound == 0)
  47. if(strcmp(attr, t->attr) == 0)
  48. linefound = 1;
  49. }
  50. }
  51. close(fd);
  52. return first;
  53. }