dirstat.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "lib.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "sys9.h"
  5. #include "dir.h"
  6. enum
  7. {
  8. DIRSIZE = STATFIXLEN + 16 * 4 /* enough for encoded stat buf + some reasonable strings */
  9. };
  10. Dir*
  11. _dirstat(char *name)
  12. {
  13. Dir *d;
  14. uchar *buf;
  15. int n, nd, i;
  16. nd = DIRSIZE;
  17. for(i=0; i<2; i++){ /* should work by the second try */
  18. d = malloc(sizeof(Dir) + BIT16SZ +nd);
  19. if(d == nil)
  20. return nil;
  21. buf = (uchar*)&d[1];
  22. n = _STAT(name, buf, BIT16SZ+nd);
  23. if(n < BIT16SZ){
  24. free(d);
  25. return nil;
  26. }
  27. nd = GBIT16((uchar*)buf); /* size needed to store whole stat buffer */
  28. if(nd <= n){
  29. _convM2D(buf, n, d, (char*)&d[1]);
  30. return d;
  31. }
  32. /* else sizeof(Dir)+BIT16SZ+nd is plenty */
  33. free(d);
  34. }
  35. return nil;
  36. }
  37. int
  38. _dirwstat(char *name, Dir *d)
  39. {
  40. uchar *buf;
  41. int r;
  42. r = _sizeD2M(d);
  43. buf = malloc(r);
  44. if(buf == nil)
  45. return -1;
  46. _convD2M(d, buf, r);
  47. r = _WSTAT(name, buf, r);
  48. free(buf);
  49. return r;
  50. }
  51. Dir*
  52. _dirfstat(int fd)
  53. {
  54. Dir *d;
  55. uchar *buf;
  56. int n, nd, i;
  57. nd = DIRSIZE;
  58. for(i=0; i<2; i++){ /* should work by the second try */
  59. d = malloc(sizeof(Dir) + nd);
  60. if(d == nil)
  61. return nil;
  62. buf = (uchar*)&d[1];
  63. n = _FSTAT(fd, buf, nd);
  64. if(n < BIT16SZ){
  65. free(d);
  66. return nil;
  67. }
  68. nd = GBIT16(buf); /* size needed to store whole stat buffer */
  69. if(nd <= n){
  70. _convM2D(buf, n, d, (char*)&d[1]);
  71. return d;
  72. }
  73. /* else sizeof(Dir)+nd is plenty */
  74. free(d);
  75. }
  76. return nil;
  77. }
  78. int
  79. _dirfwstat(int fd, Dir *d)
  80. {
  81. uchar *buf;
  82. int r;
  83. r = _sizeD2M(d);
  84. buf = malloc(r);
  85. if(buf == nil)
  86. return -1;
  87. _convD2M(d, buf, r);
  88. r = _FWSTAT(fd, buf, r);
  89. free(buf);
  90. return r;
  91. }
  92. void
  93. _nulldir(Dir *d)
  94. {
  95. memset(d, ~0, sizeof(Dir));
  96. d->name = d->uid = d->gid = d->muid = "";
  97. }