188f3d729Sakaplan /*
288f3d729Sakaplan * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
388f3d729Sakaplan * Use is subject to license terms.
488f3d729Sakaplan */
588f3d729Sakaplan
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
9*f22acdffSgbrunett /*
10*f22acdffSgbrunett * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate /*
167c478bd9Sstevel@tonic-gate * test expression
177c478bd9Sstevel@tonic-gate * [ expression ]
187c478bd9Sstevel@tonic-gate */
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include <stdio.h>
21*f22acdffSgbrunett #include <stdlib.h>
22*f22acdffSgbrunett #include <unistd.h>
237c478bd9Sstevel@tonic-gate #include <sys/types.h>
247c478bd9Sstevel@tonic-gate #include <sys/stat.h>
25*f22acdffSgbrunett #include <string.h>
26*f22acdffSgbrunett
277c478bd9Sstevel@tonic-gate #define EQ(a, b) ((strcmp(a, b) == 0))
287c478bd9Sstevel@tonic-gate
29*f22acdffSgbrunett static char *nxtarg(int mt);
30*f22acdffSgbrunett static int exp(void);
31*f22acdffSgbrunett static int e1(void);
32*f22acdffSgbrunett static int e2(void);
33*f22acdffSgbrunett static int e3(void);
34*f22acdffSgbrunett static int tio(char *a, int f);
35*f22acdffSgbrunett static int ftype(char *f, int field);
36*f22acdffSgbrunett static int filtyp(char *f, int field);
37*f22acdffSgbrunett static int fsizep(char *f);
38*f22acdffSgbrunett static void synbad(char *s1, char *s2);
397c478bd9Sstevel@tonic-gate
40*f22acdffSgbrunett static int ap;
41*f22acdffSgbrunett static int ac;
42*f22acdffSgbrunett static char **av;
437c478bd9Sstevel@tonic-gate
4488f3d729Sakaplan int
main(int argc,char * argv[])4588f3d729Sakaplan main(int argc, char *argv[])
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate int status;
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate ac = argc; av = argv; ap = 1;
507c478bd9Sstevel@tonic-gate if (EQ(argv[0], "[")) {
517c478bd9Sstevel@tonic-gate if (!EQ(argv[--ac], "]"))
527c478bd9Sstevel@tonic-gate synbad("] missing", "");
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate argv[ac] = 0;
55*f22acdffSgbrunett if (ac <= 1)
56*f22acdffSgbrunett exit(1);
577c478bd9Sstevel@tonic-gate status = (exp() ? 0 : 1);
587c478bd9Sstevel@tonic-gate if (nxtarg(1) != 0)
597c478bd9Sstevel@tonic-gate synbad("too many arguments", "");
6088f3d729Sakaplan return (status);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
63*f22acdffSgbrunett static char *
nxtarg(int mt)6488f3d729Sakaplan nxtarg(int mt)
6588f3d729Sakaplan {
667c478bd9Sstevel@tonic-gate if (ap >= ac) {
677c478bd9Sstevel@tonic-gate if (mt) {
687c478bd9Sstevel@tonic-gate ap++;
697c478bd9Sstevel@tonic-gate return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate synbad("argument expected", "");
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate return (av[ap++]);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
76*f22acdffSgbrunett static int
exp(void)7788f3d729Sakaplan exp(void)
7888f3d729Sakaplan {
797c478bd9Sstevel@tonic-gate int p1;
807c478bd9Sstevel@tonic-gate 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 if (EQ(p2, "-o"))
867c478bd9Sstevel@tonic-gate return (p1 | exp());
877c478bd9Sstevel@tonic-gate if (EQ(p2, "]"))
887c478bd9Sstevel@tonic-gate synbad("syntax error", "");
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate ap--;
917c478bd9Sstevel@tonic-gate return (p1);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
94*f22acdffSgbrunett static int
e1(void)9588f3d729Sakaplan e1(void)
9688f3d729Sakaplan {
977c478bd9Sstevel@tonic-gate int p1;
987c478bd9Sstevel@tonic-gate char *p2;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate p1 = e2();
1017c478bd9Sstevel@tonic-gate p2 = nxtarg(1);
1027c478bd9Sstevel@tonic-gate if ((p2 != 0) && EQ(p2, "-a"))
1037c478bd9Sstevel@tonic-gate return (p1 & e1());
1047c478bd9Sstevel@tonic-gate ap--;
1057c478bd9Sstevel@tonic-gate return (p1);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
108*f22acdffSgbrunett static int
e2(void)10988f3d729Sakaplan e2(void)
11088f3d729Sakaplan {
1117c478bd9Sstevel@tonic-gate if (EQ(nxtarg(0), "!"))
1127c478bd9Sstevel@tonic-gate return (!e3());
1137c478bd9Sstevel@tonic-gate ap--;
1147c478bd9Sstevel@tonic-gate return (e3());
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
117*f22acdffSgbrunett static int
e3(void)11888f3d729Sakaplan e3(void)
11988f3d729Sakaplan {
1207c478bd9Sstevel@tonic-gate int p1;
12188f3d729Sakaplan char *a;
1227c478bd9Sstevel@tonic-gate char *p2;
1237c478bd9Sstevel@tonic-gate int int1, int2;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate a = nxtarg(0);
1267c478bd9Sstevel@tonic-gate if (EQ(a, "(")) {
1277c478bd9Sstevel@tonic-gate p1 = exp();
1287c478bd9Sstevel@tonic-gate if (!EQ(nxtarg(0), ")")) synbad(") expected", "");
1297c478bd9Sstevel@tonic-gate return (p1);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate p2 = nxtarg(1);
1327c478bd9Sstevel@tonic-gate ap--;
1337c478bd9Sstevel@tonic-gate if ((p2 == 0) || (!EQ(p2, "=") && !EQ(p2, "!="))) {
1347c478bd9Sstevel@tonic-gate if (EQ(a, "-r"))
1357c478bd9Sstevel@tonic-gate return (tio(nxtarg(0), 4));
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate if (EQ(a, "-w"))
1387c478bd9Sstevel@tonic-gate return (tio(nxtarg(0), 2));
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate if (EQ(a, "-x"))
1417c478bd9Sstevel@tonic-gate return (tio(nxtarg(0), 1));
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate if (EQ(a, "-d"))
1447c478bd9Sstevel@tonic-gate return (filtyp(nxtarg(0), S_IFDIR));
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (EQ(a, "-c"))
1477c478bd9Sstevel@tonic-gate return (filtyp(nxtarg(0), S_IFCHR));
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate if (EQ(a, "-b"))
1507c478bd9Sstevel@tonic-gate return (filtyp(nxtarg(0), S_IFBLK));
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (EQ(a, "-f")) {
1537c478bd9Sstevel@tonic-gate struct stat statb;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate return (stat(nxtarg(0), &statb) >= 0 &&
1567c478bd9Sstevel@tonic-gate (statb.st_mode & S_IFMT) != S_IFDIR);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (EQ(a, "-h"))
1607c478bd9Sstevel@tonic-gate return (filtyp(nxtarg(0), S_IFLNK));
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (EQ(a, "-u"))
1637c478bd9Sstevel@tonic-gate return (ftype(nxtarg(0), S_ISUID));
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (EQ(a, "-g"))
1667c478bd9Sstevel@tonic-gate return (ftype(nxtarg(0), S_ISGID));
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (EQ(a, "-k"))
1697c478bd9Sstevel@tonic-gate return (ftype(nxtarg(0), S_ISVTX));
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if (EQ(a, "-p"))
1727c478bd9Sstevel@tonic-gate #ifdef S_IFIFO
1737c478bd9Sstevel@tonic-gate return (filtyp(nxtarg(0), S_IFIFO));
1747c478bd9Sstevel@tonic-gate #else
1757c478bd9Sstevel@tonic-gate return (nxtarg(0), 0);
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (EQ(a, "-s"))
1797c478bd9Sstevel@tonic-gate return (fsizep(nxtarg(0)));
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (EQ(a, "-t"))
1827c478bd9Sstevel@tonic-gate if (ap >= ac)
1837c478bd9Sstevel@tonic-gate return (isatty(1));
1847c478bd9Sstevel@tonic-gate else if (EQ((a = nxtarg(0)), "-a") || EQ(a, "-o")) {
1857c478bd9Sstevel@tonic-gate ap--;
1867c478bd9Sstevel@tonic-gate return (isatty(1));
1877c478bd9Sstevel@tonic-gate } else
1887c478bd9Sstevel@tonic-gate return (isatty(atoi(a)));
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if (EQ(a, "-n"))
1917c478bd9Sstevel@tonic-gate return (!EQ(nxtarg(0), ""));
1927c478bd9Sstevel@tonic-gate if (EQ(a, "-z"))
1937c478bd9Sstevel@tonic-gate return (EQ(nxtarg(0), ""));
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate p2 = nxtarg(1);
1977c478bd9Sstevel@tonic-gate if (p2 == 0)
1987c478bd9Sstevel@tonic-gate return (!EQ(a, ""));
1997c478bd9Sstevel@tonic-gate if (EQ(p2, "-a") || EQ(p2, "-o")) {
2007c478bd9Sstevel@tonic-gate ap--;
2017c478bd9Sstevel@tonic-gate return (!EQ(a, ""));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate if (EQ(p2, "="))
2047c478bd9Sstevel@tonic-gate return (EQ(nxtarg(0), a));
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if (EQ(p2, "!="))
2077c478bd9Sstevel@tonic-gate return (!EQ(nxtarg(0), a));
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate int1 = atoi(a);
2107c478bd9Sstevel@tonic-gate int2 = atoi(nxtarg(0));
2117c478bd9Sstevel@tonic-gate if (EQ(p2, "-eq"))
2127c478bd9Sstevel@tonic-gate return (int1 == int2);
2137c478bd9Sstevel@tonic-gate if (EQ(p2, "-ne"))
2147c478bd9Sstevel@tonic-gate return (int1 != int2);
2157c478bd9Sstevel@tonic-gate if (EQ(p2, "-gt"))
2167c478bd9Sstevel@tonic-gate return (int1 > int2);
2177c478bd9Sstevel@tonic-gate if (EQ(p2, "-lt"))
2187c478bd9Sstevel@tonic-gate return (int1 < int2);
2197c478bd9Sstevel@tonic-gate if (EQ(p2, "-ge"))
2207c478bd9Sstevel@tonic-gate return (int1 >= int2);
2217c478bd9Sstevel@tonic-gate if (EQ(p2, "-le"))
2227c478bd9Sstevel@tonic-gate return (int1 <= int2);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate synbad("unknown operator ", p2);
2257c478bd9Sstevel@tonic-gate /* NOTREACHED */
22688f3d729Sakaplan return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
229*f22acdffSgbrunett static int
tio(char * a,int f)23088f3d729Sakaplan tio(char *a, int f)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate if (access(a, f) == 0)
2337c478bd9Sstevel@tonic-gate return (1);
2347c478bd9Sstevel@tonic-gate else
2357c478bd9Sstevel@tonic-gate return (0);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
238*f22acdffSgbrunett static int
ftype(char * f,int field)23988f3d729Sakaplan ftype(char *f, int field)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate struct stat statb;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate if (stat(f, &statb) < 0)
2447c478bd9Sstevel@tonic-gate return (0);
2457c478bd9Sstevel@tonic-gate if ((statb.st_mode & field) == field)
2467c478bd9Sstevel@tonic-gate return (1);
2477c478bd9Sstevel@tonic-gate return (0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
250*f22acdffSgbrunett static int
filtyp(char * f,int field)25188f3d729Sakaplan filtyp(char *f, int field)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate struct stat statb;
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if (field == S_IFLNK) {
2567c478bd9Sstevel@tonic-gate if (lstat(f, &statb) < 0)
2577c478bd9Sstevel@tonic-gate return (0);
2587c478bd9Sstevel@tonic-gate } else {
2597c478bd9Sstevel@tonic-gate if (stat(f, &statb) < 0)
2607c478bd9Sstevel@tonic-gate return (0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == field)
2637c478bd9Sstevel@tonic-gate return (1);
2647c478bd9Sstevel@tonic-gate else
2657c478bd9Sstevel@tonic-gate return (0);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
268*f22acdffSgbrunett static int
fsizep(char * f)26988f3d729Sakaplan fsizep(char *f)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate struct stat statb;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate if (stat(f, &statb) < 0)
2747c478bd9Sstevel@tonic-gate return (0);
2757c478bd9Sstevel@tonic-gate return (statb.st_size > 0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
278*f22acdffSgbrunett static void
synbad(char * s1,char * s2)27988f3d729Sakaplan synbad(char *s1, char *s2)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate (void) write(2, "test: ", 6);
2827c478bd9Sstevel@tonic-gate (void) write(2, s1, strlen(s1));
2837c478bd9Sstevel@tonic-gate (void) write(2, s2, strlen(s2));
2847c478bd9Sstevel@tonic-gate (void) write(2, "\n", 1);
2857c478bd9Sstevel@tonic-gate exit(255);
2867c478bd9Sstevel@tonic-gate }
287