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