test: update from Plan 9

R=rsc
CC=plan9port.codebot
http://codereview.appspot.com/4847051
This commit is contained in:
David du Colombier 2011-08-16 16:11:48 -04:00 committed by Russ Cox
parent 11a3ce57b1
commit f6d2cbfe47
2 changed files with 81 additions and 75 deletions

View file

@ -209,3 +209,10 @@ is in the current directory.
.B \*9/src/cmd/test.c .B \*9/src/cmd/test.c
.SH "SEE ALSO" .SH "SEE ALSO"
.IR rc (1) .IR rc (1)
.SH BUGS
Won't complain about extraneous arguments
since there may be arguments left unprocessed by
short-circuit evaluation of
.B -a
or
.BR -o .

View file

@ -230,92 +230,77 @@ tio(char *a, int f)
return access (a, f) >= 0; return access (a, f) >= 0;
} }
/* copy to local memory; clear names for safety */ /*
int * note that the name strings pointed to by Dir members are
localstat(char *f, Dir *dir) * allocated with the Dir itself (by the same call to malloc),
{ * but are not included in sizeof(Dir), so copying a Dir won't
Dir *d; * copy the strings it points to.
*/
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;
}
int int
hasmode(char *f, ulong m) hasmode(char *f, ulong m)
{ {
Dir dir; int r;
Dir *dir;
if(localstat(f,&dir)<0) dir = dirstat(f);
return(0); if (dir == nil)
return(dir.mode&m); return 0;
r = (dir->mode & m) != 0;
free(dir);
return r;
} }
int int
isdir(char *f) isdir(char *f)
{ {
Dir dir; return hasmode(f, DMDIR);
if(localstat(f,&dir)<0)
return(0);
return(dir.mode&DMDIR);
} }
int int
isreg(char *f) isreg(char *f)
{ {
Dir dir; int r;
Dir *dir;
if(localstat(f,&dir)<0) dir = dirstat(f);
return(0); if (dir == nil)
return(!(dir.mode&DMDIR)); return 0;
r = (dir->mode & DMDIR) == 0;
free(dir);
return r;
} }
int int
isatty(int fd) isatty(int fd)
{ {
Dir d1, d2; int r;
Dir *d1, *d2;
if(localfstat(fd, &d1) < 0) d1 = dirfstat(fd);
return 0; d2 = dirstat("/dev/cons");
if(localstat("/dev/cons", &d2) < 0) if (d1 == nil || d2 == nil)
return 0; r = 0;
return d1.type==d2.type && d1.dev==d2.dev && d1.qid.path==d2.qid.path; else
r = d1->type == d2->type && d1->dev == d2->dev &&
d1->qid.path == d2->qid.path;
free(d1);
free(d2);
return r;
} }
int int
fsizep(char *f) fsizep(char *f)
{ {
Dir dir; int r;
Dir *dir;
if(localstat(f,&dir)<0) dir = dirstat(f);
return(0); if (dir == nil)
return(dir.length>0); return 0;
r = dir->length > 0;
free(dir);
return r;
} }
void void
@ -344,12 +329,14 @@ isint(char *s, int *pans)
int int
isolder(char *pin, char *f) isolder(char *pin, char *f)
{ {
char *p = pin; int r;
ulong n, m; ulong n, m;
Dir dir; char *p = pin;
Dir *dir;
if(localstat(f,&dir)<0) dir = dirstat(f);
return(0); if (dir == nil)
return 0;
/* parse time */ /* parse time */
n = 0; 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 int
isolderthan(char *a, char *b) isolderthan(char *a, char *b)
{ {
Dir ad, bd; int r;
Dir *ad, *bd;
if(localstat(a, &ad)<0) ad = dirstat(a);
return(0); bd = dirstat(b);
if(localstat(b, &bd)<0) if (ad == nil || bd == nil)
return(0); r = 0;
return ad.mtime > bd.mtime; else
r = ad->mtime > bd->mtime;
free(ad);
free(bd);
return r;
} }
int int
isnewerthan(char *a, char *b) isnewerthan(char *a, char *b)
{ {
Dir ad, bd; int r;
Dir *ad, *bd;
if(localstat(a, &ad)<0) ad = dirstat(a);
return(0); bd = dirstat(b);
if(localstat(b, &bd)<0) if (ad == nil || bd == nil)
return(0); r = 0;
return ad.mtime < bd.mtime; else
r = ad->mtime < bd->mtime;
free(ad);
free(bd);
return r;
} }