1*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 2*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 3*7c478bd9Sstevel@tonic-gate 4*7c478bd9Sstevel@tonic-gate 5*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1980 Regents of the University of California. 6*7c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 7*7c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 8*7c478bd9Sstevel@tonic-gate */ 9*7c478bd9Sstevel@tonic-gate 10*7c478bd9Sstevel@tonic-gate /* 11*7c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc. 12*7c478bd9Sstevel@tonic-gate * All Rights Reserved. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */ 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate /* 18*7c478bd9Sstevel@tonic-gate * test expression 19*7c478bd9Sstevel@tonic-gate * [ expression ] 20*7c478bd9Sstevel@tonic-gate */ 21*7c478bd9Sstevel@tonic-gate 22*7c478bd9Sstevel@tonic-gate #include <stdio.h> 23*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 24*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 25*7c478bd9Sstevel@tonic-gate #define EQ(a,b) ((strcmp(a,b)==0)) 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate int ap; 28*7c478bd9Sstevel@tonic-gate int ac; 29*7c478bd9Sstevel@tonic-gate char **av; 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate char *nxtarg(); 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate main(argc, argv) 34*7c478bd9Sstevel@tonic-gate char *argv[]; 35*7c478bd9Sstevel@tonic-gate { 36*7c478bd9Sstevel@tonic-gate int status; 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate ac = argc; av = argv; ap = 1; 39*7c478bd9Sstevel@tonic-gate if(EQ(argv[0],"[")) { 40*7c478bd9Sstevel@tonic-gate if(!EQ(argv[--ac],"]")) 41*7c478bd9Sstevel@tonic-gate synbad("] missing",""); 42*7c478bd9Sstevel@tonic-gate } 43*7c478bd9Sstevel@tonic-gate argv[ac] = 0; 44*7c478bd9Sstevel@tonic-gate if (ac<=1) exit(1); 45*7c478bd9Sstevel@tonic-gate status = (exp()?0:1); 46*7c478bd9Sstevel@tonic-gate if (nxtarg(1)!=0) 47*7c478bd9Sstevel@tonic-gate synbad("too many arguments",""); 48*7c478bd9Sstevel@tonic-gate exit(status); 49*7c478bd9Sstevel@tonic-gate } 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate char *nxtarg(mt) { 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate if (ap>=ac) { 54*7c478bd9Sstevel@tonic-gate if(mt) { 55*7c478bd9Sstevel@tonic-gate ap++; 56*7c478bd9Sstevel@tonic-gate return(0); 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate synbad("argument expected",""); 59*7c478bd9Sstevel@tonic-gate } 60*7c478bd9Sstevel@tonic-gate return(av[ap++]); 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate exp() { 64*7c478bd9Sstevel@tonic-gate int p1; 65*7c478bd9Sstevel@tonic-gate char *p2; 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate p1 = e1(); 68*7c478bd9Sstevel@tonic-gate p2 = nxtarg(1); 69*7c478bd9Sstevel@tonic-gate if (p2 != 0) { 70*7c478bd9Sstevel@tonic-gate if (EQ(p2, "-o")) 71*7c478bd9Sstevel@tonic-gate return(p1 | exp()); 72*7c478bd9Sstevel@tonic-gate if (EQ(p2, "]")) 73*7c478bd9Sstevel@tonic-gate synbad("syntax error",""); 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate ap--; 76*7c478bd9Sstevel@tonic-gate return(p1); 77*7c478bd9Sstevel@tonic-gate } 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate e1() { 80*7c478bd9Sstevel@tonic-gate int p1; 81*7c478bd9Sstevel@tonic-gate char *p2; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate p1 = e2(); 84*7c478bd9Sstevel@tonic-gate p2 = nxtarg(1); 85*7c478bd9Sstevel@tonic-gate if ((p2 != 0) && EQ(p2, "-a")) 86*7c478bd9Sstevel@tonic-gate return(p1 & e1()); 87*7c478bd9Sstevel@tonic-gate ap--; 88*7c478bd9Sstevel@tonic-gate return(p1); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate e2() { 92*7c478bd9Sstevel@tonic-gate if (EQ(nxtarg(0), "!")) 93*7c478bd9Sstevel@tonic-gate return(!e3()); 94*7c478bd9Sstevel@tonic-gate ap--; 95*7c478bd9Sstevel@tonic-gate return(e3()); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate e3() { 99*7c478bd9Sstevel@tonic-gate int p1; 100*7c478bd9Sstevel@tonic-gate register char *a; 101*7c478bd9Sstevel@tonic-gate char *p2; 102*7c478bd9Sstevel@tonic-gate int int1, int2; 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate a=nxtarg(0); 105*7c478bd9Sstevel@tonic-gate if(EQ(a, "(")) { 106*7c478bd9Sstevel@tonic-gate p1 = exp(); 107*7c478bd9Sstevel@tonic-gate if(!EQ(nxtarg(0), ")")) synbad(") expected",""); 108*7c478bd9Sstevel@tonic-gate return(p1); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate p2 = nxtarg(1); 111*7c478bd9Sstevel@tonic-gate ap--; 112*7c478bd9Sstevel@tonic-gate if ((p2 == 0) || (!EQ(p2, "=") && !EQ(p2, "!="))) { 113*7c478bd9Sstevel@tonic-gate if(EQ(a, "-r")) 114*7c478bd9Sstevel@tonic-gate return(tio(nxtarg(0), 4)); 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if(EQ(a, "-w")) 117*7c478bd9Sstevel@tonic-gate return(tio(nxtarg(0), 2)); 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate if(EQ(a, "-x")) 120*7c478bd9Sstevel@tonic-gate return(tio(nxtarg(0), 1)); 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate if(EQ(a, "-d")) 123*7c478bd9Sstevel@tonic-gate return(filtyp(nxtarg(0), S_IFDIR)); 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if(EQ(a, "-c")) 126*7c478bd9Sstevel@tonic-gate return(filtyp(nxtarg(0), S_IFCHR)); 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate if(EQ(a, "-b")) 129*7c478bd9Sstevel@tonic-gate return(filtyp(nxtarg(0), S_IFBLK)); 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if(EQ(a, "-f")) { 132*7c478bd9Sstevel@tonic-gate struct stat statb; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate return(stat(nxtarg(0), &statb) >= 0 && 135*7c478bd9Sstevel@tonic-gate (statb.st_mode & S_IFMT) != S_IFDIR); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate if(EQ(a, "-h")) 139*7c478bd9Sstevel@tonic-gate return(filtyp(nxtarg(0), S_IFLNK)); 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate if(EQ(a, "-u")) 142*7c478bd9Sstevel@tonic-gate return(ftype(nxtarg(0), S_ISUID)); 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate if(EQ(a, "-g")) 145*7c478bd9Sstevel@tonic-gate return(ftype(nxtarg(0), S_ISGID)); 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate if(EQ(a, "-k")) 148*7c478bd9Sstevel@tonic-gate return(ftype(nxtarg(0), S_ISVTX)); 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate if(EQ(a, "-p")) 151*7c478bd9Sstevel@tonic-gate #ifdef S_IFIFO 152*7c478bd9Sstevel@tonic-gate return(filtyp(nxtarg(0), S_IFIFO)); 153*7c478bd9Sstevel@tonic-gate #else 154*7c478bd9Sstevel@tonic-gate return(nxtarg(0), 0); 155*7c478bd9Sstevel@tonic-gate #endif 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate if(EQ(a, "-s")) 158*7c478bd9Sstevel@tonic-gate return(fsizep(nxtarg(0))); 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate if(EQ(a, "-t")) 161*7c478bd9Sstevel@tonic-gate if(ap>=ac) 162*7c478bd9Sstevel@tonic-gate return(isatty(1)); 163*7c478bd9Sstevel@tonic-gate else if (EQ((a = nxtarg(0)), "-a") || EQ(a, "-o")) { 164*7c478bd9Sstevel@tonic-gate ap--; 165*7c478bd9Sstevel@tonic-gate return(isatty(1)); 166*7c478bd9Sstevel@tonic-gate } else 167*7c478bd9Sstevel@tonic-gate return(isatty(atoi(a))); 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate if(EQ(a, "-n")) 170*7c478bd9Sstevel@tonic-gate return(!EQ(nxtarg(0), "")); 171*7c478bd9Sstevel@tonic-gate if(EQ(a, "-z")) 172*7c478bd9Sstevel@tonic-gate return(EQ(nxtarg(0), "")); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate p2 = nxtarg(1); 176*7c478bd9Sstevel@tonic-gate if (p2==0) 177*7c478bd9Sstevel@tonic-gate return(!EQ(a,"")); 178*7c478bd9Sstevel@tonic-gate if (EQ(p2, "-a") || EQ(p2, "-o")) { 179*7c478bd9Sstevel@tonic-gate ap--; 180*7c478bd9Sstevel@tonic-gate return(!EQ(a,"")); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate if(EQ(p2, "=")) 183*7c478bd9Sstevel@tonic-gate return(EQ(nxtarg(0), a)); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate if(EQ(p2, "!=")) 186*7c478bd9Sstevel@tonic-gate return(!EQ(nxtarg(0), a)); 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate int1 = atoi(a); 189*7c478bd9Sstevel@tonic-gate int2 = atoi(nxtarg(0)); 190*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-eq")) 191*7c478bd9Sstevel@tonic-gate return(int1==int2); 192*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-ne")) 193*7c478bd9Sstevel@tonic-gate return(int1!=int2); 194*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-gt")) 195*7c478bd9Sstevel@tonic-gate return(int1>int2); 196*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-lt")) 197*7c478bd9Sstevel@tonic-gate return(int1<int2); 198*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-ge")) 199*7c478bd9Sstevel@tonic-gate return(int1>=int2); 200*7c478bd9Sstevel@tonic-gate if(EQ(p2, "-le")) 201*7c478bd9Sstevel@tonic-gate return(int1<=int2); 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate synbad("unknown operator ",p2); 204*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate tio(a, f) 208*7c478bd9Sstevel@tonic-gate char *a; 209*7c478bd9Sstevel@tonic-gate int f; 210*7c478bd9Sstevel@tonic-gate { 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate if (access(a, f) == 0) 213*7c478bd9Sstevel@tonic-gate return(1); 214*7c478bd9Sstevel@tonic-gate else 215*7c478bd9Sstevel@tonic-gate return(0); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate ftype(f, field) 219*7c478bd9Sstevel@tonic-gate char *f; 220*7c478bd9Sstevel@tonic-gate int field; 221*7c478bd9Sstevel@tonic-gate { 222*7c478bd9Sstevel@tonic-gate struct stat statb; 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate if(stat(f,&statb)<0) 225*7c478bd9Sstevel@tonic-gate return(0); 226*7c478bd9Sstevel@tonic-gate if((statb.st_mode&field)==field) 227*7c478bd9Sstevel@tonic-gate return(1); 228*7c478bd9Sstevel@tonic-gate return(0); 229*7c478bd9Sstevel@tonic-gate } 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate filtyp(f, field) 232*7c478bd9Sstevel@tonic-gate char *f; 233*7c478bd9Sstevel@tonic-gate int field; 234*7c478bd9Sstevel@tonic-gate { 235*7c478bd9Sstevel@tonic-gate struct stat statb; 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate if (field == S_IFLNK) { 238*7c478bd9Sstevel@tonic-gate if(lstat(f,&statb)<0) 239*7c478bd9Sstevel@tonic-gate return(0); 240*7c478bd9Sstevel@tonic-gate } else { 241*7c478bd9Sstevel@tonic-gate if(stat(f,&statb)<0) 242*7c478bd9Sstevel@tonic-gate return(0); 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate if((statb.st_mode&S_IFMT)==field) 245*7c478bd9Sstevel@tonic-gate return(1); 246*7c478bd9Sstevel@tonic-gate else 247*7c478bd9Sstevel@tonic-gate return(0); 248*7c478bd9Sstevel@tonic-gate } 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate fsizep(f) 251*7c478bd9Sstevel@tonic-gate char *f; 252*7c478bd9Sstevel@tonic-gate { 253*7c478bd9Sstevel@tonic-gate struct stat statb; 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate if(stat(f,&statb)<0) 256*7c478bd9Sstevel@tonic-gate return(0); 257*7c478bd9Sstevel@tonic-gate return(statb.st_size>0); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate synbad(s1,s2) 261*7c478bd9Sstevel@tonic-gate char *s1, *s2; 262*7c478bd9Sstevel@tonic-gate { 263*7c478bd9Sstevel@tonic-gate (void) write(2, "test: ", 6); 264*7c478bd9Sstevel@tonic-gate (void) write(2, s1, strlen(s1)); 265*7c478bd9Sstevel@tonic-gate (void) write(2, s2, strlen(s2)); 266*7c478bd9Sstevel@tonic-gate (void) write(2, "\n", 1); 267*7c478bd9Sstevel@tonic-gate exit(255); 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate length(s) 271*7c478bd9Sstevel@tonic-gate char *s; 272*7c478bd9Sstevel@tonic-gate { 273*7c478bd9Sstevel@tonic-gate char *es=s; 274*7c478bd9Sstevel@tonic-gate while(*es++); 275*7c478bd9Sstevel@tonic-gate return(es-s-1); 276*7c478bd9Sstevel@tonic-gate } 277