read9pmsg.c 462 B

12345678910111213141516171819202122232425262728293031
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <fcall.h>
  4. int
  5. read9pmsg(int fd, void *abuf, uint n)
  6. {
  7. int m, len;
  8. uchar *buf;
  9. buf = abuf;
  10. /* read count */
  11. m = readn(fd, buf, BIT32SZ);
  12. if(m != BIT32SZ){
  13. if(m < 0)
  14. return -1;
  15. return 0;
  16. }
  17. len = GBIT32(buf);
  18. if(len <= BIT32SZ || len > n){
  19. werrstr("bad length in 9P2000 message header");
  20. return -1;
  21. }
  22. len -= BIT32SZ;
  23. m = readn(fd, buf+BIT32SZ, len);
  24. if(m < len)
  25. return 0;
  26. return BIT32SZ+m;
  27. }