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