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