xref: /titanic_41/usr/src/cmd/sh/test.c (revision 39e7390a771e8a17884d4f1558a2a41422b38104)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*39e7390aSna195498  * Common Development and Distribution License (the "License").
6*39e7390aSna195498  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*39e7390aSna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26*39e7390aSna195498 
27965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005c8Schin /*	  All Rights Reserved  	*/
29965005c8Schin 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *      test expression
347c478bd9Sstevel@tonic-gate  *      [ expression ]
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include	"defs.h"
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern	int lstat();
42*39e7390aSna195498 
437c478bd9Sstevel@tonic-gate int	ap, ac;
447c478bd9Sstevel@tonic-gate unsigned char **av;
457c478bd9Sstevel@tonic-gate 
46965005c8Schin int
test(int argn,unsigned char * com[])47965005c8Schin test(int argn, unsigned char *com[])
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	ac = argn;
507c478bd9Sstevel@tonic-gate 	av = com;
517c478bd9Sstevel@tonic-gate 	ap = 1;
527c478bd9Sstevel@tonic-gate 	if (eq(com[0],"["))
537c478bd9Sstevel@tonic-gate 	{
547c478bd9Sstevel@tonic-gate 		if (!eq(com[--ac], "]"))
55*39e7390aSna195498 			failed((unsigned char *)"test", nobracket);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 	com[ac] = 0;
587c478bd9Sstevel@tonic-gate 	if (ac <= 1)
597c478bd9Sstevel@tonic-gate 		return(1);
607c478bd9Sstevel@tonic-gate 	return(exp() ? 0 : 1);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate unsigned char *
nxtarg(mt)647c478bd9Sstevel@tonic-gate nxtarg(mt)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	if (ap >= ac)
677c478bd9Sstevel@tonic-gate 	{
687c478bd9Sstevel@tonic-gate 		if (mt)
697c478bd9Sstevel@tonic-gate 		{
707c478bd9Sstevel@tonic-gate 			ap++;
717c478bd9Sstevel@tonic-gate 			return(0);
727c478bd9Sstevel@tonic-gate 		}
73*39e7390aSna195498 		failed((unsigned char *)"test", noarg);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	return(av[ap++]);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
78965005c8Schin int
exp(void)79965005c8Schin exp(void)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	int	p1;
827c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	p1 = e1();
857c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
867c478bd9Sstevel@tonic-gate 	if (p2 != 0)
877c478bd9Sstevel@tonic-gate 	{
887c478bd9Sstevel@tonic-gate 		if (eq(p2, "-o"))
897c478bd9Sstevel@tonic-gate 			return(p1 | exp());
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		/* if (!eq(p2, ")"))
92965005c8Schin 			failed((unsigned char *)"test", synmsg); */
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	ap--;
957c478bd9Sstevel@tonic-gate 	return(p1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
98965005c8Schin int
e1(void)99965005c8Schin e1(void)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int	p1;
1027c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	p1 = e2();
1057c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if ((p2 != 0) && eq(p2, "-a"))
1087c478bd9Sstevel@tonic-gate 		return(p1 & e1());
1097c478bd9Sstevel@tonic-gate 	ap--;
1107c478bd9Sstevel@tonic-gate 	return(p1);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
113965005c8Schin int
e2(void)114965005c8Schin e2(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	if (eq(nxtarg(0), "!"))
1177c478bd9Sstevel@tonic-gate 		return(!e3());
1187c478bd9Sstevel@tonic-gate 	ap--;
1197c478bd9Sstevel@tonic-gate 	return(e3());
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
122965005c8Schin int
e3(void)123965005c8Schin e3(void)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int	p1;
126965005c8Schin 	unsigned char	*a;
1277c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
1287c478bd9Sstevel@tonic-gate 	longlong_t	ll_1, ll_2;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	a = nxtarg(0);
1317c478bd9Sstevel@tonic-gate 	if (eq(a, "("))
1327c478bd9Sstevel@tonic-gate 	{
1337c478bd9Sstevel@tonic-gate 		p1 = exp();
1347c478bd9Sstevel@tonic-gate 		if (!eq(nxtarg(0), ")"))
135*39e7390aSna195498 			failed((unsigned char *)"test", noparen);
1367c478bd9Sstevel@tonic-gate 		return(p1);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1397c478bd9Sstevel@tonic-gate 	ap--;
1407c478bd9Sstevel@tonic-gate 	if ((p2 == 0) || (!eq(p2, "=") && !eq(p2, "!=")))
1417c478bd9Sstevel@tonic-gate 	{
1427c478bd9Sstevel@tonic-gate 		if (eq(a, "-r"))
1437c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IREAD, 0) == 0);
1447c478bd9Sstevel@tonic-gate 		if (eq(a, "-w"))
1457c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IWRITE, 0) == 0);
1467c478bd9Sstevel@tonic-gate 		if (eq(a, "-x"))
1477c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IEXEC, 0) == 0);
1487c478bd9Sstevel@tonic-gate 		if (eq(a, "-d"))
1497c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFDIR));
1507c478bd9Sstevel@tonic-gate 		if (eq(a, "-c"))
1517c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFCHR));
1527c478bd9Sstevel@tonic-gate 		if (eq(a, "-b"))
1537c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFBLK));
1547c478bd9Sstevel@tonic-gate 		if (eq(a, "-f"))
1557c478bd9Sstevel@tonic-gate 			if (ucb_builtins) {
1567c478bd9Sstevel@tonic-gate 				struct stat statb;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 				return(stat((char *)nxtarg(0), &statb) >= 0 &&
1597c478bd9Sstevel@tonic-gate 					(statb.st_mode & S_IFMT) != S_IFDIR);
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 			else
1627c478bd9Sstevel@tonic-gate 				return(filtyp(nxtarg(0), S_IFREG));
1637c478bd9Sstevel@tonic-gate 		if (eq(a, "-u"))
1647c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISUID));
1657c478bd9Sstevel@tonic-gate 		if (eq(a, "-g"))
1667c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISGID));
1677c478bd9Sstevel@tonic-gate 		if (eq(a, "-k"))
1687c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISVTX));
1697c478bd9Sstevel@tonic-gate 		if (eq(a, "-p"))
1707c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFIFO));
1717c478bd9Sstevel@tonic-gate 		if (eq(a, "-h") || eq(a, "-L"))
1727c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFLNK));
1737c478bd9Sstevel@tonic-gate    		if (eq(a, "-s"))
1747c478bd9Sstevel@tonic-gate 			return(fsizep(nxtarg(0)));
1757c478bd9Sstevel@tonic-gate 		if (eq(a, "-t"))
1767c478bd9Sstevel@tonic-gate 		{
1777c478bd9Sstevel@tonic-gate 			if (ap >= ac)		/* no args */
1787c478bd9Sstevel@tonic-gate 				return(isatty(1));
1797c478bd9Sstevel@tonic-gate 			else if (eq((a = nxtarg(0)), "-a") || eq(a, "-o"))
1807c478bd9Sstevel@tonic-gate 			{
1817c478bd9Sstevel@tonic-gate 				ap--;
1827c478bd9Sstevel@tonic-gate 				return(isatty(1));
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 			else
1857c478bd9Sstevel@tonic-gate 				return(isatty(atoi((char *)a)));
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		if (eq(a, "-n"))
1887c478bd9Sstevel@tonic-gate 			return(!eq(nxtarg(0), ""));
1897c478bd9Sstevel@tonic-gate 		if (eq(a, "-z"))
1907c478bd9Sstevel@tonic-gate 			return(eq(nxtarg(0), ""));
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1947c478bd9Sstevel@tonic-gate 	if (p2 == 0)
1957c478bd9Sstevel@tonic-gate 		return(!eq(a, ""));
1967c478bd9Sstevel@tonic-gate 	if (eq(p2, "-a") || eq(p2, "-o"))
1977c478bd9Sstevel@tonic-gate 	{
1987c478bd9Sstevel@tonic-gate 		ap--;
1997c478bd9Sstevel@tonic-gate 		return(!eq(a, ""));
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (eq(p2, "="))
2027c478bd9Sstevel@tonic-gate 		return(eq(nxtarg(0), a));
2037c478bd9Sstevel@tonic-gate 	if (eq(p2, "!="))
2047c478bd9Sstevel@tonic-gate 		return(!eq(nxtarg(0), a));
2057c478bd9Sstevel@tonic-gate 	ll_1 = strtoll((char *)a, NULL, 10);
2067c478bd9Sstevel@tonic-gate 	ll_2 = strtoll((char *)nxtarg(0), NULL, 10);
2077c478bd9Sstevel@tonic-gate 	if (eq(p2, "-eq"))
2087c478bd9Sstevel@tonic-gate 		return (ll_1 == ll_2);
2097c478bd9Sstevel@tonic-gate 	if (eq(p2, "-ne"))
2107c478bd9Sstevel@tonic-gate 		return (ll_1 != ll_2);
2117c478bd9Sstevel@tonic-gate 	if (eq(p2, "-gt"))
2127c478bd9Sstevel@tonic-gate 		return (ll_1 > ll_2);
2137c478bd9Sstevel@tonic-gate 	if (eq(p2, "-lt"))
2147c478bd9Sstevel@tonic-gate 		return (ll_1 < ll_2);
2157c478bd9Sstevel@tonic-gate 	if (eq(p2, "-ge"))
2167c478bd9Sstevel@tonic-gate 		return (ll_1 >= ll_2);
2177c478bd9Sstevel@tonic-gate 	if (eq(p2, "-le"))
2187c478bd9Sstevel@tonic-gate 		return (ll_1 <= ll_2);
2197c478bd9Sstevel@tonic-gate 
220*39e7390aSna195498 	bfailed((unsigned char *)btest, badop, p2);
2217c478bd9Sstevel@tonic-gate /* NOTREACHED */
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
224965005c8Schin int
ftype(unsigned char * f,int field)225965005c8Schin ftype(unsigned char *f, int field)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	struct stat statb;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2307c478bd9Sstevel@tonic-gate 		return(0);
2317c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & field) == field)
2327c478bd9Sstevel@tonic-gate 		return(1);
2337c478bd9Sstevel@tonic-gate 	return(0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
236965005c8Schin int
filtyp(unsigned char * f,int field)237965005c8Schin filtyp(unsigned char *f, int field)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	struct stat statb;
2407c478bd9Sstevel@tonic-gate 	int (*statf)() = (field == S_IFLNK) ? lstat : stat;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if ((*statf)(f, &statb) < 0)
2437c478bd9Sstevel@tonic-gate 		return(0);
2447c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == field)
2457c478bd9Sstevel@tonic-gate 		return(1);
2467c478bd9Sstevel@tonic-gate 	else
2477c478bd9Sstevel@tonic-gate 		return(0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
251965005c8Schin int
fsizep(unsigned char * f)252965005c8Schin fsizep(unsigned char *f)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	struct stat statb;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2577c478bd9Sstevel@tonic-gate 		return(0);
2587c478bd9Sstevel@tonic-gate 	return(statb.st_size > 0);
2597c478bd9Sstevel@tonic-gate }
260