aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/test.c
diff options
context:
space:
mode:
authorDavid du Colombier <0intro@gmail.com>2011-08-16 16:11:48 -0400
committerRuss Cox <rsc@swtch.com>2011-08-16 16:11:48 -0400
commitf6d2cbfe4795015d252ef9c6d281adccf069438f (patch)
tree5ec078f2fd809d0d06602227b406f7c508f0d208 /src/cmd/test.c
parent11a3ce57b1bb19192acd653ccee5039159f7727e (diff)
downloadplan9port-f6d2cbfe4795015d252ef9c6d281adccf069438f.tar.gz
plan9port-f6d2cbfe4795015d252ef9c6d281adccf069438f.tar.bz2
plan9port-f6d2cbfe4795015d252ef9c6d281adccf069438f.zip
test: update from Plan 9
R=rsc CC=plan9port.codebot http://codereview.appspot.com/4847051
Diffstat (limited to 'src/cmd/test.c')
-rw-r--r--src/cmd/test.c155
1 files changed, 77 insertions, 78 deletions
diff --git a/src/cmd/test.c b/src/cmd/test.c
index 6ab01b18..b782df76 100644
--- a/src/cmd/test.c
+++ b/src/cmd/test.c
@@ -230,92 +230,77 @@ tio(char *a, int f)
return access (a, f) >= 0;
}
-/* copy to local memory; clear names for safety */
-int
-localstat(char *f, Dir *dir)
-{
- Dir *d;
-
- d = dirstat(f);
- if(d == nil)
- return(-1);
- *dir = *d;
- free(d);
- dir->name = 0;
- dir->uid = 0;
- dir->gid = 0;
- dir->muid = 0;
- return 0;
-}
-
-/* copy to local memory; clear names for safety */
-int
-localfstat(int f, Dir *dir)
-{
- Dir *d;
-
- d = dirfstat(f);
- if(d == nil)
- return(-1);
- *dir = *d;
- free(d);
- dir->name = 0;
- dir->uid = 0;
- dir->gid = 0;
- dir->muid = 0;
- return 0;
-}
+/*
+ * note that the name strings pointed to by Dir members are
+ * allocated with the Dir itself (by the same call to malloc),
+ * but are not included in sizeof(Dir), so copying a Dir won't
+ * copy the strings it points to.
+ */
int
hasmode(char *f, ulong m)
{
- Dir dir;
+ int r;
+ Dir *dir;
- if(localstat(f,&dir)<0)
- return(0);
- return(dir.mode&m);
+ dir = dirstat(f);
+ if (dir == nil)
+ return 0;
+ r = (dir->mode & m) != 0;
+ free(dir);
+ return r;
}
int
isdir(char *f)
{
- Dir dir;
-
- if(localstat(f,&dir)<0)
- return(0);
- return(dir.mode&DMDIR);
+ return hasmode(f, DMDIR);
}
int
isreg(char *f)
{
- Dir dir;
+ int r;
+ Dir *dir;
- if(localstat(f,&dir)<0)
- return(0);
- return(!(dir.mode&DMDIR));
+ dir = dirstat(f);
+ if (dir == nil)
+ return 0;
+ r = (dir->mode & DMDIR) == 0;
+ free(dir);
+ return r;
}
int
isatty(int fd)
{
- Dir d1, d2;
-
- if(localfstat(fd, &d1) < 0)
- return 0;
- if(localstat("/dev/cons", &d2) < 0)
- return 0;
- return d1.type==d2.type && d1.dev==d2.dev && d1.qid.path==d2.qid.path;
+ int r;
+ Dir *d1, *d2;
+
+ d1 = dirfstat(fd);
+ d2 = dirstat("/dev/cons");
+ if (d1 == nil || d2 == nil)
+ r = 0;
+ else
+ r = d1->type == d2->type && d1->dev == d2->dev &&
+ d1->qid.path == d2->qid.path;
+ free(d1);
+ free(d2);
+ return r;
}
int
fsizep(char *f)
{
- Dir dir;
+ int r;
+ Dir *dir;
- if(localstat(f,&dir)<0)
- return(0);
- return(dir.length>0);
+ dir = dirstat(f);
+ if (dir == nil)
+ return 0;
+ r = dir->length > 0;
+ free(dir);
+ return r;
}
void
@@ -344,12 +329,14 @@ isint(char *s, int *pans)
int
isolder(char *pin, char *f)
{
- char *p = pin;
+ int r;
ulong n, m;
- Dir dir;
+ char *p = pin;
+ Dir *dir;
- if(localstat(f,&dir)<0)
- return(0);
+ dir = dirstat(f);
+ if (dir == nil)
+ return 0;
/* parse time */
n = 0;
@@ -383,29 +370,41 @@ isolder(char *pin, char *f)
}
}
- return(dir.mtime+n < time(0));
+ r = dir->mtime + n < time(0);
+ free(dir);
+ return r;
}
int
isolderthan(char *a, char *b)
{
- Dir ad, bd;
-
- if(localstat(a, &ad)<0)
- return(0);
- if(localstat(b, &bd)<0)
- return(0);
- return ad.mtime > bd.mtime;
+ int r;
+ Dir *ad, *bd;
+
+ ad = dirstat(a);
+ bd = dirstat(b);
+ if (ad == nil || bd == nil)
+ r = 0;
+ else
+ r = ad->mtime > bd->mtime;
+ free(ad);
+ free(bd);
+ return r;
}
int
isnewerthan(char *a, char *b)
{
- Dir ad, bd;
-
- if(localstat(a, &ad)<0)
- return(0);
- if(localstat(b, &bd)<0)
- return(0);
- return ad.mtime < bd.mtime;
+ int r;
+ Dir *ad, *bd;
+
+ ad = dirstat(a);
+ bd = dirstat(b);
+ if (ad == nil || bd == nil)
+ r = 0;
+ else
+ r = ad->mtime < bd->mtime;
+ free(ad);
+ free(bd);
+ return r;
}