From e5888a1ffdae813d7575f5fb02275c6bb07e5199 Mon Sep 17 00:00:00 2001 From: Taru Karttunen Date: Wed, 30 Mar 2011 15:46:40 +0300 Subject: Import sources from 2011-03-30 iso image --- sys/src/ape/lib/ap/plan9/dirstat.c | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 sys/src/ape/lib/ap/plan9/dirstat.c (limited to 'sys/src/ape/lib/ap/plan9/dirstat.c') diff --git a/sys/src/ape/lib/ap/plan9/dirstat.c b/sys/src/ape/lib/ap/plan9/dirstat.c new file mode 100755 index 000000000..f054b4f6e --- /dev/null +++ b/sys/src/ape/lib/ap/plan9/dirstat.c @@ -0,0 +1,107 @@ +#include "lib.h" +#include +#include +#include "sys9.h" +#include "dir.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); /* size needed to store whole stat buffer */ + if(nd <= n){ + _convM2D(buf, n, d, (char*)&d[1]); + return d; + } + /* else sizeof(Dir)+BIT16SZ+nd is plenty */ + free(d); + } + return nil; +} + +int +_dirwstat(char *name, Dir *d) +{ + uchar *buf; + int r; + + r = _sizeD2M(d); + buf = malloc(r); + if(buf == nil) + return -1; + _convD2M(d, buf, r); + r = _WSTAT(name, buf, r); + free(buf); + return r; +} + +Dir* +_dirfstat(int fd) +{ + 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) + nd); + if(d == nil) + return nil; + buf = (uchar*)&d[1]; + n = _FSTAT(fd, buf, nd); + if(n < BIT16SZ){ + free(d); + return nil; + } + nd = GBIT16(buf); /* size needed to store whole stat buffer */ + if(nd <= n){ + _convM2D(buf, n, d, (char*)&d[1]); + return d; + } + /* else sizeof(Dir)+nd is plenty */ + free(d); + } + return nil; +} + +int +_dirfwstat(int fd, Dir *d) +{ + uchar *buf; + int r; + + r = _sizeD2M(d); + buf = malloc(r); + if(buf == nil) + return -1; + _convD2M(d, buf, r); + r = _FWSTAT(fd, buf, r); + free(buf); + return r; +} + +void +_nulldir(Dir *d) +{ + memset(d, ~0, sizeof(Dir)); + d->name = d->uid = d->gid = d->muid = ""; +} -- cgit v1.2.3