xref: /onnv-gate/usr/src/cmd/sh/test.c (revision 2256:a9ca1c675600)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2256Sna195498  * Common Development and Distribution License (the "License").
6*2256Sna195498  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*2256Sna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
26*2256Sna195498 
27527Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28527Schin /*	  All Rights Reserved  	*/
29527Schin 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *      test expression
340Sstevel@tonic-gate  *      [ expression ]
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include	"defs.h"
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern	int lstat();
42*2256Sna195498 
430Sstevel@tonic-gate int	ap, ac;
440Sstevel@tonic-gate unsigned char **av;
450Sstevel@tonic-gate 
46527Schin int
test(int argn,unsigned char * com[])47527Schin test(int argn, unsigned char *com[])
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	ac = argn;
500Sstevel@tonic-gate 	av = com;
510Sstevel@tonic-gate 	ap = 1;
520Sstevel@tonic-gate 	if (eq(com[0],"["))
530Sstevel@tonic-gate 	{
540Sstevel@tonic-gate 		if (!eq(com[--ac], "]"))
55*2256Sna195498 			failed((unsigned char *)"test", nobracket);
560Sstevel@tonic-gate 	}
570Sstevel@tonic-gate 	com[ac] = 0;
580Sstevel@tonic-gate 	if (ac <= 1)
590Sstevel@tonic-gate 		return(1);
600Sstevel@tonic-gate 	return(exp() ? 0 : 1);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate unsigned char *
nxtarg(mt)640Sstevel@tonic-gate nxtarg(mt)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	if (ap >= ac)
670Sstevel@tonic-gate 	{
680Sstevel@tonic-gate 		if (mt)
690Sstevel@tonic-gate 		{
700Sstevel@tonic-gate 			ap++;
710Sstevel@tonic-gate 			return(0);
720Sstevel@tonic-gate 		}
73*2256Sna195498 		failed((unsigned char *)"test", noarg);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	return(av[ap++]);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
78527Schin int
exp(void)79527Schin exp(void)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	int	p1;
820Sstevel@tonic-gate 	unsigned char	*p2;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	p1 = e1();
850Sstevel@tonic-gate 	p2 = nxtarg(1);
860Sstevel@tonic-gate 	if (p2 != 0)
870Sstevel@tonic-gate 	{
880Sstevel@tonic-gate 		if (eq(p2, "-o"))
890Sstevel@tonic-gate 			return(p1 | exp());
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		/* if (!eq(p2, ")"))
92527Schin 			failed((unsigned char *)"test", synmsg); */
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	ap--;
950Sstevel@tonic-gate 	return(p1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
98527Schin int
e1(void)99527Schin e1(void)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int	p1;
1020Sstevel@tonic-gate 	unsigned char	*p2;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	p1 = e2();
1050Sstevel@tonic-gate 	p2 = nxtarg(1);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if ((p2 != 0) && eq(p2, "-a"))
1080Sstevel@tonic-gate 		return(p1 & e1());
1090Sstevel@tonic-gate 	ap--;
1100Sstevel@tonic-gate 	return(p1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
113527Schin int
e2(void)114527Schin e2(void)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	if (eq(nxtarg(0), "!"))
1170Sstevel@tonic-gate 		return(!e3());
1180Sstevel@tonic-gate 	ap--;
1190Sstevel@tonic-gate 	return(e3());
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
122527Schin int
e3(void)123527Schin e3(void)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	int	p1;
126527Schin 	unsigned char	*a;
1270Sstevel@tonic-gate 	unsigned char	*p2;
1280Sstevel@tonic-gate 	longlong_t	ll_1, ll_2;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	a = nxtarg(0);
1310Sstevel@tonic-gate 	if (eq(a, "("))
1320Sstevel@tonic-gate 	{
1330Sstevel@tonic-gate 		p1 = exp();
1340Sstevel@tonic-gate 		if (!eq(nxtarg(0), ")"))
135*2256Sna195498 			failed((unsigned char *)"test", noparen);
1360Sstevel@tonic-gate 		return(p1);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	p2 = nxtarg(1);
1390Sstevel@tonic-gate 	ap--;
1400Sstevel@tonic-gate 	if ((p2 == 0) || (!eq(p2, "=") && !eq(p2, "!=")))
1410Sstevel@tonic-gate 	{
1420Sstevel@tonic-gate 		if (eq(a, "-r"))
1430Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IREAD, 0) == 0);
1440Sstevel@tonic-gate 		if (eq(a, "-w"))
1450Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IWRITE, 0) == 0);
1460Sstevel@tonic-gate 		if (eq(a, "-x"))
1470Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IEXEC, 0) == 0);
1480Sstevel@tonic-gate 		if (eq(a, "-d"))
1490Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFDIR));
1500Sstevel@tonic-gate 		if (eq(a, "-c"))
1510Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFCHR));
1520Sstevel@tonic-gate 		if (eq(a, "-b"))
1530Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFBLK));
1540Sstevel@tonic-gate 		if (eq(a, "-f"))
1550Sstevel@tonic-gate 			if (ucb_builtins) {
1560Sstevel@tonic-gate 				struct stat statb;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 				return(stat((char *)nxtarg(0), &statb) >= 0 &&
1590Sstevel@tonic-gate 					(statb.st_mode & S_IFMT) != S_IFDIR);
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 			else
1620Sstevel@tonic-gate 				return(filtyp(nxtarg(0), S_IFREG));
1630Sstevel@tonic-gate 		if (eq(a, "-u"))
1640Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISUID));
1650Sstevel@tonic-gate 		if (eq(a, "-g"))
1660Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISGID));
1670Sstevel@tonic-gate 		if (eq(a, "-k"))
1680Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISVTX));
1690Sstevel@tonic-gate 		if (eq(a, "-p"))
1700Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFIFO));
1710Sstevel@tonic-gate 		if (eq(a, "-h") || eq(a, "-L"))
1720Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFLNK));
1730Sstevel@tonic-gate    		if (eq(a, "-s"))
1740Sstevel@tonic-gate 			return(fsizep(nxtarg(0)));
1750Sstevel@tonic-gate 		if (eq(a, "-t"))
1760Sstevel@tonic-gate 		{
1770Sstevel@tonic-gate 			if (ap >= ac)		/* no args */
1780Sstevel@tonic-gate 				return(isatty(1));
1790Sstevel@tonic-gate 			else if (eq((a = nxtarg(0)), "-a") || eq(a, "-o"))
1800Sstevel@tonic-gate 			{
1810Sstevel@tonic-gate 				ap--;
1820Sstevel@tonic-gate 				return(isatty(1));
1830Sstevel@tonic-gate 			}
1840Sstevel@tonic-gate 			else
1850Sstevel@tonic-gate 				return(isatty(atoi((char *)a)));
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		if (eq(a, "-n"))
1880Sstevel@tonic-gate 			return(!eq(nxtarg(0), ""));
1890Sstevel@tonic-gate 		if (eq(a, "-z"))
1900Sstevel@tonic-gate 			return(eq(nxtarg(0), ""));
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	p2 = nxtarg(1);
1940Sstevel@tonic-gate 	if (p2 == 0)
1950Sstevel@tonic-gate 		return(!eq(a, ""));
1960Sstevel@tonic-gate 	if (eq(p2, "-a") || eq(p2, "-o"))
1970Sstevel@tonic-gate 	{
1980Sstevel@tonic-gate 		ap--;
1990Sstevel@tonic-gate 		return(!eq(a, ""));
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (eq(p2, "="))
2020Sstevel@tonic-gate 		return(eq(nxtarg(0), a));
2030Sstevel@tonic-gate 	if (eq(p2, "!="))
2040Sstevel@tonic-gate 		return(!eq(nxtarg(0), a));
2050Sstevel@tonic-gate 	ll_1 = strtoll((char *)a, NULL, 10);
2060Sstevel@tonic-gate 	ll_2 = strtoll((char *)nxtarg(0), NULL, 10);
2070Sstevel@tonic-gate 	if (eq(p2, "-eq"))
2080Sstevel@tonic-gate 		return (ll_1 == ll_2);
2090Sstevel@tonic-gate 	if (eq(p2, "-ne"))
2100Sstevel@tonic-gate 		return (ll_1 != ll_2);
2110Sstevel@tonic-gate 	if (eq(p2, "-gt"))
2120Sstevel@tonic-gate 		return (ll_1 > ll_2);
2130Sstevel@tonic-gate 	if (eq(p2, "-lt"))
2140Sstevel@tonic-gate 		return (ll_1 < ll_2);
2150Sstevel@tonic-gate 	if (eq(p2, "-ge"))
2160Sstevel@tonic-gate 		return (ll_1 >= ll_2);
2170Sstevel@tonic-gate 	if (eq(p2, "-le"))
2180Sstevel@tonic-gate 		return (ll_1 <= ll_2);
2190Sstevel@tonic-gate 
220*2256Sna195498 	bfailed((unsigned char *)btest, badop, p2);
2210Sstevel@tonic-gate /* NOTREACHED */
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
224527Schin int
ftype(unsigned char * f,int field)225527Schin ftype(unsigned char *f, int field)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	struct stat statb;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2300Sstevel@tonic-gate 		return(0);
2310Sstevel@tonic-gate 	if ((statb.st_mode & field) == field)
2320Sstevel@tonic-gate 		return(1);
2330Sstevel@tonic-gate 	return(0);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
236527Schin int
filtyp(unsigned char * f,int field)237527Schin filtyp(unsigned char *f, int field)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	struct stat statb;
2400Sstevel@tonic-gate 	int (*statf)() = (field == S_IFLNK) ? lstat : stat;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if ((*statf)(f, &statb) < 0)
2430Sstevel@tonic-gate 		return(0);
2440Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == field)
2450Sstevel@tonic-gate 		return(1);
2460Sstevel@tonic-gate 	else
2470Sstevel@tonic-gate 		return(0);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 
251527Schin int
fsizep(unsigned char * f)252527Schin fsizep(unsigned char *f)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	struct stat statb;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2570Sstevel@tonic-gate 		return(0);
2580Sstevel@tonic-gate 	return(statb.st_size > 0);
2590Sstevel@tonic-gate }
260