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