summaryrefslogtreecommitdiff
path: root/sys/src/libc/9sys/dirstat.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libc/9sys/dirstat.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libc/9sys/dirstat.c')
-rwxr-xr-xsys/src/libc/9sys/dirstat.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/sys/src/libc/9sys/dirstat.c b/sys/src/libc/9sys/dirstat.c
new file mode 100755
index 000000000..eb5171d2a
--- /dev/null
+++ b/sys/src/libc/9sys/dirstat.c
@@ -0,0 +1,37 @@
+#include <u.h>
+#include <libc.h>
+#include <fcall.h>
+
+enum
+{
+ DIRSIZE = STATFIXLEN + 16 * 4 /* enough for encoded stat buf + some reasonable strings */
+};
+
+Dir*
+dirstat(char *name)
+{
+ Dir *d;
+ uchar *buf;
+ int n, nd, i;
+
+ nd = DIRSIZE;
+ for(i=0; i<2; i++){ /* should work by the second try */
+ d = malloc(sizeof(Dir) + BIT16SZ + nd);
+ if(d == nil)
+ return nil;
+ buf = (uchar*)&d[1];
+ n = stat(name, buf, BIT16SZ+nd);
+ if(n < BIT16SZ){
+ free(d);
+ return nil;
+ }
+ nd = GBIT16((uchar*)buf); /* upper bound on size of Dir + strings */
+ if(nd <= n){
+ convM2D(buf, n, d, (char*)&d[1]);
+ return d;
+ }
+ /* else sizeof(Dir)+BIT16SZ+nd is plenty */
+ free(d);
+ }
+ return nil;
+}