1: /* 2: * test expression 3: * [ expression ] 4: */ 5: 6: #include <stdio.h> 7: #include <sys/types.h> 8: #include <sys/stat.h> 9: #define EQ(a,b) ((tmp=a)==0?0:(strcmp(tmp,b)==0)) 10: 11: #define DIR 1 12: #define FIL 2 13: int ap; 14: int ac; 15: char **av; 16: char *tmp; 17: 18: main(argc, argv) 19: char *argv[]; 20: { 21: 22: ac = argc; av = argv; ap = 1; 23: if(EQ(argv[0],"[")) { 24: if(!EQ(argv[--ac],"]")) 25: synbad("] missing",""); 26: } 27: argv[ac] = 0; 28: if (ac<=1) exit(1); 29: exit(exp()?0:1); 30: } 31: 32: char *nxtarg(mt) { 33: 34: if (ap>=ac) { 35: if(mt) { 36: ap++; 37: return(0); 38: } 39: synbad("argument expected",""); 40: } 41: return(av[ap++]); 42: } 43: 44: exp() { 45: int p1; 46: 47: p1 = e1(); 48: if (EQ(nxtarg(1), "-o")) return(p1 | exp()); 49: ap--; 50: return(p1); 51: } 52: 53: e1() { 54: int p1; 55: 56: p1 = e2(); 57: if (EQ(nxtarg(1), "-a")) return (p1 & e1()); 58: ap--; 59: return(p1); 60: } 61: 62: e2() { 63: if (EQ(nxtarg(0), "!")) 64: return(!e3()); 65: ap--; 66: return(e3()); 67: } 68: 69: e3() { 70: int p1; 71: register char *a; 72: char *p2; 73: int int1, int2; 74: 75: a=nxtarg(0); 76: if(EQ(a, "(")) { 77: p1 = exp(); 78: if(!EQ(nxtarg(0), ")")) synbad(") expected",""); 79: return(p1); 80: } 81: 82: if(EQ(a, "-r")) 83: return(tio(nxtarg(0), 0)); 84: 85: if(EQ(a, "-w")) 86: return(tio(nxtarg(0), 1)); 87: 88: if(EQ(a, "-d")) 89: return(ftype(nxtarg(0))==DIR); 90: 91: if(EQ(a, "-f")) 92: return(ftype(nxtarg(0))==FIL); 93: 94: if(EQ(a, "-s")) 95: return(fsizep(nxtarg(0))); 96: 97: if(EQ(a, "-t")) 98: if(ap>=ac) 99: return(isatty(1)); 100: else 101: return(isatty(atoi(nxtarg(0)))); 102: 103: if(EQ(a, "-n")) 104: return(!EQ(nxtarg(0), "")); 105: if(EQ(a, "-z")) 106: return(EQ(nxtarg(0), "")); 107: 108: p2 = nxtarg(1); 109: if (p2==0) 110: return(!EQ(a,"")); 111: if(EQ(p2, "=")) 112: return(EQ(nxtarg(0), a)); 113: 114: if(EQ(p2, "!=")) 115: return(!EQ(nxtarg(0), a)); 116: 117: if(EQ(a, "-l")) { 118: int1=length(p2); 119: p2=nxtarg(0); 120: } else{ int1=atoi(a); 121: } 122: int2 = atoi(nxtarg(0)); 123: if(EQ(p2, "-eq")) 124: return(int1==int2); 125: if(EQ(p2, "-ne")) 126: return(int1!=int2); 127: if(EQ(p2, "-gt")) 128: return(int1>int2); 129: if(EQ(p2, "-lt")) 130: return(int1<int2); 131: if(EQ(p2, "-ge")) 132: return(int1>=int2); 133: if(EQ(p2, "-le")) 134: return(int1<=int2); 135: 136: synbad("unknown operator ",p2); 137: } 138: 139: tio(a, f) 140: char *a; 141: int f; 142: { 143: 144: f = open(a, f); 145: if (f>=0) { 146: close(f); 147: return(1); 148: } 149: return(0); 150: } 151: 152: ftype(f) 153: char *f; 154: { 155: struct stat statb; 156: 157: if(stat(f,&statb)<0) 158: return(0); 159: if((statb.st_mode&S_IFMT)==S_IFDIR) 160: return(DIR); 161: return(FIL); 162: } 163: 164: fsizep(f) 165: char *f; 166: { 167: struct stat statb; 168: if(stat(f,&statb)<0) 169: return(0); 170: return(statb.st_size>0); 171: } 172: 173: synbad(s1,s2) 174: char *s1, *s2; 175: { 176: write(2, "test: ", 6); 177: write(2, s1, strlen(s1)); 178: write(2, s2, strlen(s2)); 179: write(2, "\n", 1); 180: exit(255); 181: } 182: 183: length(s) 184: char *s; 185: { 186: char *es=s; 187: while(*es++); 188: return(es-s-1); 189: }