dirstat.c 688 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <fcall.h>
  4. enum
  5. {
  6. DIRSIZE = STATFIXLEN + 16 * 4 /* enough for encoded stat buf + some reasonable strings */
  7. };
  8. Dir*
  9. dirstat(char *name)
  10. {
  11. Dir *d;
  12. uchar *buf;
  13. int n, nd, i;
  14. nd = DIRSIZE;
  15. for(i=0; i<2; i++){ /* should work by the second try */
  16. d = malloc(sizeof(Dir) + BIT16SZ + nd);
  17. if(d == nil)
  18. return nil;
  19. buf = (uchar*)&d[1];
  20. n = stat(name, buf, BIT16SZ+nd);
  21. if(n < BIT16SZ){
  22. free(d);
  23. return nil;
  24. }
  25. nd = GBIT16((uchar*)buf); /* upper bound on size of Dir + strings */
  26. if(nd <= n){
  27. convM2D(buf, n, d, (char*)&d[1]);
  28. return d;
  29. }
  30. /* else sizeof(Dir)+BIT16SZ+nd is plenty */
  31. free(d);
  32. }
  33. return nil;
  34. }