test.c revision 88f3d7297a011d0fa0768a7b2082305a822487b2
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Use is subject to license terms.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* All Rights Reserved */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Copyright (c) 1980 Regents of the University of California.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * All rights reserved. The Berkeley software License Agreement
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * specifies the terms and conditions for redistribution.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#pragma ident "%Z%%M% %I% %E% SMI"
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * test expression
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * [ expression ]
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <stdio.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/types.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/stat.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#define EQ(a,b) ((strcmp(a,b)==0))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint ap;
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint ac;
4bff34e37def8a90f9194d81bc345c52ba20086athurlowchar **av;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowvoid synbad(char *s1, char *s2);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint tio(char *a, int f);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint ftype(char *f, int field);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint filtyp(char *f, int field);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint fsizep(char *f);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowchar *nxtarg(int mt);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowmain(int argc, char *argv[])
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int status;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ac = argc; av = argv; ap = 1;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if(EQ(argv[0],"[")) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if(!EQ(argv[--ac],"]"))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow synbad("] missing","");
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow argv[ac] = 0;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (ac<=1) exit(1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow status = (exp()?0:1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (nxtarg(1)!=0)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow synbad("too many arguments","");
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (status);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowchar *
4bff34e37def8a90f9194d81bc345c52ba20086athurlownxtarg(int mt)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (ap>=ac) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if(mt) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ap++;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(0);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow synbad("argument expected","");
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(av[ap++]);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowexp(void)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int p1;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *p2;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p1 = e1();
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p2 = nxtarg(1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (p2 != 0) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (EQ(p2, "-o"))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(p1 | exp());
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (EQ(p2, "]"))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow synbad("syntax error","");
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ap--;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(p1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowe1(void)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int p1;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *p2;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p1 = e2();
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p2 = nxtarg(1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if ((p2 != 0) && EQ(p2, "-a"))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(p1 & e1());
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ap--;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(p1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowe2(void)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (EQ(nxtarg(0), "!"))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(!e3());
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ap--;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(e3());
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowe3(void)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int p1;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *a;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *p2;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int int1, int2;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow a=nxtarg(0);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if(EQ(a, "(")) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p1 = exp();
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if(!EQ(nxtarg(0), ")")) synbad(") expected","");
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return(p1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow p2 = nxtarg(1);
ap--;
if ((p2 == 0) || (!EQ(p2, "=") && !EQ(p2, "!="))) {
if(EQ(a, "-r"))
return(tio(nxtarg(0), 4));
if(EQ(a, "-w"))
return(tio(nxtarg(0), 2));
if(EQ(a, "-x"))
return(tio(nxtarg(0), 1));
if(EQ(a, "-d"))
return(filtyp(nxtarg(0), S_IFDIR));
if(EQ(a, "-c"))
return(filtyp(nxtarg(0), S_IFCHR));
if(EQ(a, "-b"))
return(filtyp(nxtarg(0), S_IFBLK));
if(EQ(a, "-f")) {
struct stat statb;
return(stat(nxtarg(0), &statb) >= 0 &&
(statb.st_mode & S_IFMT) != S_IFDIR);
}
if(EQ(a, "-h"))
return(filtyp(nxtarg(0), S_IFLNK));
if(EQ(a, "-u"))
return(ftype(nxtarg(0), S_ISUID));
if(EQ(a, "-g"))
return(ftype(nxtarg(0), S_ISGID));
if(EQ(a, "-k"))
return(ftype(nxtarg(0), S_ISVTX));
if(EQ(a, "-p"))
#ifdef S_IFIFO
return(filtyp(nxtarg(0), S_IFIFO));
#else
return(nxtarg(0), 0);
#endif
if(EQ(a, "-s"))
return(fsizep(nxtarg(0)));
if(EQ(a, "-t"))
if(ap>=ac)
return(isatty(1));
else if (EQ((a = nxtarg(0)), "-a") || EQ(a, "-o")) {
ap--;
return(isatty(1));
} else
return(isatty(atoi(a)));
if(EQ(a, "-n"))
return(!EQ(nxtarg(0), ""));
if(EQ(a, "-z"))
return(EQ(nxtarg(0), ""));
}
p2 = nxtarg(1);
if (p2==0)
return(!EQ(a,""));
if (EQ(p2, "-a") || EQ(p2, "-o")) {
ap--;
return(!EQ(a,""));
}
if(EQ(p2, "="))
return(EQ(nxtarg(0), a));
if(EQ(p2, "!="))
return(!EQ(nxtarg(0), a));
int1 = atoi(a);
int2 = atoi(nxtarg(0));
if(EQ(p2, "-eq"))
return(int1==int2);
if(EQ(p2, "-ne"))
return(int1!=int2);
if(EQ(p2, "-gt"))
return(int1>int2);
if(EQ(p2, "-lt"))
return(int1<int2);
if(EQ(p2, "-ge"))
return(int1>=int2);
if(EQ(p2, "-le"))
return(int1<=int2);
synbad("unknown operator ",p2);
/* NOTREACHED */
return (0);
}
int
tio(char *a, int f)
{
if (access(a, f) == 0)
return(1);
else
return(0);
}
int
ftype(char *f, int field)
{
struct stat statb;
if(stat(f,&statb)<0)
return(0);
if((statb.st_mode&field)==field)
return(1);
return(0);
}
int
filtyp(char *f, int field)
{
struct stat statb;
if (field == S_IFLNK) {
if(lstat(f,&statb)<0)
return(0);
} else {
if(stat(f,&statb)<0)
return(0);
}
if((statb.st_mode&S_IFMT)==field)
return(1);
else
return(0);
}
int
fsizep(char *f)
{
struct stat statb;
if(stat(f,&statb)<0)
return(0);
return(statb.st_size>0);
}
void
synbad(char *s1, char *s2)
{
(void) write(2, "test: ", 6);
(void) write(2, s1, strlen(s1));
(void) write(2, s2, strlen(s2));
(void) write(2, "\n", 1);
exit(255);
}
int
length(char *s)
{
char *es=s;
while(*es++);
return(es-s-1);
}