xref: /illumos-gate/usr/src/cmd/sh/test.c (revision 08335216)
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
539e7390aSna  * Common Development and Distribution License (the "License").
639e7390aSna  * 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 /*
2339e7390aSna  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
2639e7390aSna 
27965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005c8Schin /*	  All Rights Reserved  	*/
29965005c8Schin 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *      test expression
327c478bd9Sstevel@tonic-gate  *      [ expression ]
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern	int lstat();
4039e7390aSna 
417c478bd9Sstevel@tonic-gate int	ap, ac;
427c478bd9Sstevel@tonic-gate unsigned char **av;
437c478bd9Sstevel@tonic-gate 
44965005c8Schin int
test(int argn,unsigned char * com[])45965005c8Schin test(int argn, unsigned char *com[])
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	ac = argn;
487c478bd9Sstevel@tonic-gate 	av = com;
497c478bd9Sstevel@tonic-gate 	ap = 1;
507c478bd9Sstevel@tonic-gate 	if (eq(com[0],"["))
517c478bd9Sstevel@tonic-gate 	{
527c478bd9Sstevel@tonic-gate 		if (!eq(com[--ac], "]"))
5339e7390aSna 			failed((unsigned char *)"test", nobracket);
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 	com[ac] = 0;
567c478bd9Sstevel@tonic-gate 	if (ac <= 1)
577c478bd9Sstevel@tonic-gate 		return(1);
587c478bd9Sstevel@tonic-gate 	return(exp() ? 0 : 1);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate unsigned char *
nxtarg(int mt)62*08335216SToomas Soome nxtarg(int mt)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	if (ap >= ac)
657c478bd9Sstevel@tonic-gate 	{
667c478bd9Sstevel@tonic-gate 		if (mt)
677c478bd9Sstevel@tonic-gate 		{
687c478bd9Sstevel@tonic-gate 			ap++;
697c478bd9Sstevel@tonic-gate 			return(0);
707c478bd9Sstevel@tonic-gate 		}
7139e7390aSna 		failed((unsigned char *)"test", noarg);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	return(av[ap++]);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
76965005c8Schin int
exp(void)77965005c8Schin exp(void)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	int	p1;
807c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	p1 = e1();
837c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
847c478bd9Sstevel@tonic-gate 	if (p2 != 0)
857c478bd9Sstevel@tonic-gate 	{
867c478bd9Sstevel@tonic-gate 		if (eq(p2, "-o"))
877c478bd9Sstevel@tonic-gate 			return(p1 | exp());
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		/* if (!eq(p2, ")"))
90965005c8Schin 			failed((unsigned char *)"test", synmsg); */
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	ap--;
937c478bd9Sstevel@tonic-gate 	return(p1);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
96965005c8Schin int
e1(void)97965005c8Schin e1(void)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	int	p1;
1007c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	p1 = e2();
1037c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((p2 != 0) && eq(p2, "-a"))
1067c478bd9Sstevel@tonic-gate 		return(p1 & e1());
1077c478bd9Sstevel@tonic-gate 	ap--;
1087c478bd9Sstevel@tonic-gate 	return(p1);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
111965005c8Schin int
e2(void)112965005c8Schin e2(void)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	if (eq(nxtarg(0), "!"))
1157c478bd9Sstevel@tonic-gate 		return(!e3());
1167c478bd9Sstevel@tonic-gate 	ap--;
1177c478bd9Sstevel@tonic-gate 	return(e3());
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
120965005c8Schin int
e3(void)121965005c8Schin e3(void)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	int	p1;
124965005c8Schin 	unsigned char	*a;
1257c478bd9Sstevel@tonic-gate 	unsigned char	*p2;
1267c478bd9Sstevel@tonic-gate 	longlong_t	ll_1, ll_2;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	a = nxtarg(0);
1297c478bd9Sstevel@tonic-gate 	if (eq(a, "("))
1307c478bd9Sstevel@tonic-gate 	{
1317c478bd9Sstevel@tonic-gate 		p1 = exp();
1327c478bd9Sstevel@tonic-gate 		if (!eq(nxtarg(0), ")"))
13339e7390aSna 			failed((unsigned char *)"test", noparen);
1347c478bd9Sstevel@tonic-gate 		return(p1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1377c478bd9Sstevel@tonic-gate 	ap--;
1387c478bd9Sstevel@tonic-gate 	if ((p2 == 0) || (!eq(p2, "=") && !eq(p2, "!=")))
1397c478bd9Sstevel@tonic-gate 	{
1407c478bd9Sstevel@tonic-gate 		if (eq(a, "-r"))
1417c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IREAD, 0) == 0);
1427c478bd9Sstevel@tonic-gate 		if (eq(a, "-w"))
1437c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IWRITE, 0) == 0);
1447c478bd9Sstevel@tonic-gate 		if (eq(a, "-x"))
1457c478bd9Sstevel@tonic-gate 			return(chk_access(nxtarg(0), S_IEXEC, 0) == 0);
1467c478bd9Sstevel@tonic-gate 		if (eq(a, "-d"))
1477c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFDIR));
1487c478bd9Sstevel@tonic-gate 		if (eq(a, "-c"))
1497c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFCHR));
1507c478bd9Sstevel@tonic-gate 		if (eq(a, "-b"))
1517c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFBLK));
1527c478bd9Sstevel@tonic-gate 		if (eq(a, "-f"))
1537c478bd9Sstevel@tonic-gate 			if (ucb_builtins) {
1547c478bd9Sstevel@tonic-gate 				struct stat statb;
1552a8bcb4eSToomas Soome 
1567c478bd9Sstevel@tonic-gate 				return(stat((char *)nxtarg(0), &statb) >= 0 &&
1577c478bd9Sstevel@tonic-gate 					(statb.st_mode & S_IFMT) != S_IFDIR);
1587c478bd9Sstevel@tonic-gate 			}
1597c478bd9Sstevel@tonic-gate 			else
1607c478bd9Sstevel@tonic-gate 				return(filtyp(nxtarg(0), S_IFREG));
1617c478bd9Sstevel@tonic-gate 		if (eq(a, "-u"))
1627c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISUID));
1637c478bd9Sstevel@tonic-gate 		if (eq(a, "-g"))
1647c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISGID));
1657c478bd9Sstevel@tonic-gate 		if (eq(a, "-k"))
1667c478bd9Sstevel@tonic-gate 			return(ftype(nxtarg(0), S_ISVTX));
1677c478bd9Sstevel@tonic-gate 		if (eq(a, "-p"))
1687c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFIFO));
1697c478bd9Sstevel@tonic-gate 		if (eq(a, "-h") || eq(a, "-L"))
1707c478bd9Sstevel@tonic-gate 			return(filtyp(nxtarg(0), S_IFLNK));
1717c478bd9Sstevel@tonic-gate    		if (eq(a, "-s"))
1727c478bd9Sstevel@tonic-gate 			return(fsizep(nxtarg(0)));
1737c478bd9Sstevel@tonic-gate 		if (eq(a, "-t"))
1747c478bd9Sstevel@tonic-gate 		{
1757c478bd9Sstevel@tonic-gate 			if (ap >= ac)		/* no args */
1767c478bd9Sstevel@tonic-gate 				return(isatty(1));
1777c478bd9Sstevel@tonic-gate 			else if (eq((a = nxtarg(0)), "-a") || eq(a, "-o"))
1787c478bd9Sstevel@tonic-gate 			{
1797c478bd9Sstevel@tonic-gate 				ap--;
1807c478bd9Sstevel@tonic-gate 				return(isatty(1));
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			else
1837c478bd9Sstevel@tonic-gate 				return(isatty(atoi((char *)a)));
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		if (eq(a, "-n"))
1867c478bd9Sstevel@tonic-gate 			return(!eq(nxtarg(0), ""));
1877c478bd9Sstevel@tonic-gate 		if (eq(a, "-z"))
1887c478bd9Sstevel@tonic-gate 			return(eq(nxtarg(0), ""));
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	p2 = nxtarg(1);
1927c478bd9Sstevel@tonic-gate 	if (p2 == 0)
1937c478bd9Sstevel@tonic-gate 		return(!eq(a, ""));
1947c478bd9Sstevel@tonic-gate 	if (eq(p2, "-a") || eq(p2, "-o"))
1957c478bd9Sstevel@tonic-gate 	{
1967c478bd9Sstevel@tonic-gate 		ap--;
1977c478bd9Sstevel@tonic-gate 		return(!eq(a, ""));
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	if (eq(p2, "="))
2007c478bd9Sstevel@tonic-gate 		return(eq(nxtarg(0), a));
2017c478bd9Sstevel@tonic-gate 	if (eq(p2, "!="))
2027c478bd9Sstevel@tonic-gate 		return(!eq(nxtarg(0), a));
2037c478bd9Sstevel@tonic-gate 	ll_1 = strtoll((char *)a, NULL, 10);
2047c478bd9Sstevel@tonic-gate 	ll_2 = strtoll((char *)nxtarg(0), NULL, 10);
2057c478bd9Sstevel@tonic-gate 	if (eq(p2, "-eq"))
2067c478bd9Sstevel@tonic-gate 		return (ll_1 == ll_2);
2077c478bd9Sstevel@tonic-gate 	if (eq(p2, "-ne"))
2087c478bd9Sstevel@tonic-gate 		return (ll_1 != ll_2);
2097c478bd9Sstevel@tonic-gate 	if (eq(p2, "-gt"))
2107c478bd9Sstevel@tonic-gate 		return (ll_1 > ll_2);
2117c478bd9Sstevel@tonic-gate 	if (eq(p2, "-lt"))
2127c478bd9Sstevel@tonic-gate 		return (ll_1 < ll_2);
2137c478bd9Sstevel@tonic-gate 	if (eq(p2, "-ge"))
2147c478bd9Sstevel@tonic-gate 		return (ll_1 >= ll_2);
2157c478bd9Sstevel@tonic-gate 	if (eq(p2, "-le"))
2167c478bd9Sstevel@tonic-gate 		return (ll_1 <= ll_2);
2177c478bd9Sstevel@tonic-gate 
21839e7390aSna 	bfailed((unsigned char *)btest, badop, p2);
2197c478bd9Sstevel@tonic-gate /* NOTREACHED */
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
222965005c8Schin int
ftype(unsigned char * f,int field)223965005c8Schin ftype(unsigned char *f, int field)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	struct stat statb;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2287c478bd9Sstevel@tonic-gate 		return(0);
2297c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & field) == field)
2307c478bd9Sstevel@tonic-gate 		return(1);
2317c478bd9Sstevel@tonic-gate 	return(0);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
234965005c8Schin int
filtyp(unsigned char * f,int field)235965005c8Schin filtyp(unsigned char *f, int field)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	struct stat statb;
2387c478bd9Sstevel@tonic-gate 	int (*statf)() = (field == S_IFLNK) ? lstat : stat;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if ((*statf)(f, &statb) < 0)
2417c478bd9Sstevel@tonic-gate 		return(0);
2427c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == field)
2437c478bd9Sstevel@tonic-gate 		return(1);
2447c478bd9Sstevel@tonic-gate 	else
2457c478bd9Sstevel@tonic-gate 		return(0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
249965005c8Schin int
fsizep(unsigned char * f)250965005c8Schin fsizep(unsigned char *f)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	struct stat statb;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (stat((char *)f, &statb) < 0)
2557c478bd9Sstevel@tonic-gate 		return(0);
2567c478bd9Sstevel@tonic-gate 	return(statb.st_size > 0);
2577c478bd9Sstevel@tonic-gate }
258