xref: /illumos-gate/usr/src/cmd/sh/test.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *      test expression
34  *      [ expression ]
35  */
36 
37 #include	"defs.h"
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 
41 extern	int lstat();
42 int	ap, ac;
43 unsigned char **av;
44 
45 test(argn, com)
46 unsigned char *com[];
47 int	argn;
48 {
49 	ac = argn;
50 	av = com;
51 	ap = 1;
52 	if (eq(com[0],"["))
53 	{
54 		if (!eq(com[--ac], "]"))
55 			failed("test", "] missing");
56 	}
57 	com[ac] = 0;
58 	if (ac <= 1)
59 		return(1);
60 	return(exp() ? 0 : 1);
61 }
62 
63 unsigned char *
64 nxtarg(mt)
65 {
66 	if (ap >= ac)
67 	{
68 		if (mt)
69 		{
70 			ap++;
71 			return(0);
72 		}
73 		failed("test", "argument expected");
74 	}
75 	return(av[ap++]);
76 }
77 
78 exp()
79 {
80 	int	p1;
81 	unsigned char	*p2;
82 
83 	p1 = e1();
84 	p2 = nxtarg(1);
85 	if (p2 != 0)
86 	{
87 		if (eq(p2, "-o"))
88 			return(p1 | exp());
89 
90 		/* if (!eq(p2, ")"))
91 			failed("test", synmsg); */
92 	}
93 	ap--;
94 	return(p1);
95 }
96 
97 e1()
98 {
99 	int	p1;
100 	unsigned char	*p2;
101 
102 	p1 = e2();
103 	p2 = nxtarg(1);
104 
105 	if ((p2 != 0) && eq(p2, "-a"))
106 		return(p1 & e1());
107 	ap--;
108 	return(p1);
109 }
110 
111 e2()
112 {
113 	if (eq(nxtarg(0), "!"))
114 		return(!e3());
115 	ap--;
116 	return(e3());
117 }
118 
119 e3()
120 {
121 	int	p1;
122 	register unsigned char	*a;
123 	unsigned char	*p2;
124 	longlong_t	ll_1, ll_2;
125 
126 	a = nxtarg(0);
127 	if (eq(a, "("))
128 	{
129 		p1 = exp();
130 		if (!eq(nxtarg(0), ")"))
131 			failed("test",") expected");
132 		return(p1);
133 	}
134 	p2 = nxtarg(1);
135 	ap--;
136 	if ((p2 == 0) || (!eq(p2, "=") && !eq(p2, "!=")))
137 	{
138 		if (eq(a, "-r"))
139 			return(chk_access(nxtarg(0), S_IREAD, 0) == 0);
140 		if (eq(a, "-w"))
141 			return(chk_access(nxtarg(0), S_IWRITE, 0) == 0);
142 		if (eq(a, "-x"))
143 			return(chk_access(nxtarg(0), S_IEXEC, 0) == 0);
144 		if (eq(a, "-d"))
145 			return(filtyp(nxtarg(0), S_IFDIR));
146 		if (eq(a, "-c"))
147 			return(filtyp(nxtarg(0), S_IFCHR));
148 		if (eq(a, "-b"))
149 			return(filtyp(nxtarg(0), S_IFBLK));
150 		if (eq(a, "-f"))
151 			if (ucb_builtins) {
152 				struct stat statb;
153 
154 				return(stat((char *)nxtarg(0), &statb) >= 0 &&
155 					(statb.st_mode & S_IFMT) != S_IFDIR);
156 			}
157 			else
158 				return(filtyp(nxtarg(0), S_IFREG));
159 		if (eq(a, "-u"))
160 			return(ftype(nxtarg(0), S_ISUID));
161 		if (eq(a, "-g"))
162 			return(ftype(nxtarg(0), S_ISGID));
163 		if (eq(a, "-k"))
164 			return(ftype(nxtarg(0), S_ISVTX));
165 		if (eq(a, "-p"))
166 			return(filtyp(nxtarg(0), S_IFIFO));
167 		if (eq(a, "-h") || eq(a, "-L"))
168 			return(filtyp(nxtarg(0), S_IFLNK));
169    		if (eq(a, "-s"))
170 			return(fsizep(nxtarg(0)));
171 		if (eq(a, "-t"))
172 		{
173 			if (ap >= ac)		/* no args */
174 				return(isatty(1));
175 			else if (eq((a = nxtarg(0)), "-a") || eq(a, "-o"))
176 			{
177 				ap--;
178 				return(isatty(1));
179 			}
180 			else
181 				return(isatty(atoi((char *)a)));
182 		}
183 		if (eq(a, "-n"))
184 			return(!eq(nxtarg(0), ""));
185 		if (eq(a, "-z"))
186 			return(eq(nxtarg(0), ""));
187 	}
188 
189 	p2 = nxtarg(1);
190 	if (p2 == 0)
191 		return(!eq(a, ""));
192 	if (eq(p2, "-a") || eq(p2, "-o"))
193 	{
194 		ap--;
195 		return(!eq(a, ""));
196 	}
197 	if (eq(p2, "="))
198 		return(eq(nxtarg(0), a));
199 	if (eq(p2, "!="))
200 		return(!eq(nxtarg(0), a));
201 	ll_1 = strtoll((char *)a, NULL, 10);
202 	ll_2 = strtoll((char *)nxtarg(0), NULL, 10);
203 	if (eq(p2, "-eq"))
204 		return (ll_1 == ll_2);
205 	if (eq(p2, "-ne"))
206 		return (ll_1 != ll_2);
207 	if (eq(p2, "-gt"))
208 		return (ll_1 > ll_2);
209 	if (eq(p2, "-lt"))
210 		return (ll_1 < ll_2);
211 	if (eq(p2, "-ge"))
212 		return (ll_1 >= ll_2);
213 	if (eq(p2, "-le"))
214 		return (ll_1 <= ll_2);
215 
216 	bfailed(btest, badop, p2);
217 /* NOTREACHED */
218 }
219 
220 
221 ftype(f, field)
222 unsigned char	*f;
223 int	field;
224 {
225 	struct stat statb;
226 
227 	if (stat((char *)f, &statb) < 0)
228 		return(0);
229 	if ((statb.st_mode & field) == field)
230 		return(1);
231 	return(0);
232 }
233 
234 filtyp(f,field)
235 unsigned char	*f;
236 int field;
237 {
238 	struct stat statb;
239 	int (*statf)() = (field == S_IFLNK) ? lstat : stat;
240 
241 	if ((*statf)(f, &statb) < 0)
242 		return(0);
243 	if ((statb.st_mode & S_IFMT) == field)
244 		return(1);
245 	else
246 		return(0);
247 }
248 
249 
250 
251 fsizep(f)
252 unsigned char *f;
253 {
254 	struct stat statb;
255 
256 	if (stat((char *)f, &statb) < 0)
257 		return(0);
258 	return(statb.st_size > 0);
259 }
260 
261 /*
262  * fake diagnostics to continue to look like original
263  * test(1) diagnostics
264  */
265 bfailed(s1, s2, s3)
266 unsigned char *s1;
267 unsigned char *s2;
268 unsigned char *s3;
269 {
270 	prp();
271 	prs(s1);
272 	if (s2)
273 	{
274 		prs(colon);
275 		prs(s2);
276 		prs(s3);
277 	}
278 	newline();
279 	exitsh(ERROR);
280 }
281