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