4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/types.h>
47 test(int argn
, unsigned char *com
[])
54 if (!eq(com
[--ac
], "]"))
55 failed((unsigned char *)"test", nobracket
);
60 return(exp() ? 0 : 1);
73 failed((unsigned char *)"test", noarg
);
92 failed((unsigned char *)"test", synmsg); */
107 if ((p2
!= 0) && eq(p2
, "-a"))
116 if (eq(nxtarg(0), "!"))
128 longlong_t ll_1
, ll_2
;
134 if (!eq(nxtarg(0), ")"))
135 failed((unsigned char *)"test", noparen
);
140 if ((p2
== 0) || (!eq(p2
, "=") && !eq(p2
, "!=")))
143 return(chk_access(nxtarg(0), S_IREAD
, 0) == 0);
145 return(chk_access(nxtarg(0), S_IWRITE
, 0) == 0);
147 return(chk_access(nxtarg(0), S_IEXEC
, 0) == 0);
149 return(filtyp(nxtarg(0), S_IFDIR
));
151 return(filtyp(nxtarg(0), S_IFCHR
));
153 return(filtyp(nxtarg(0), S_IFBLK
));
158 return(stat((char *)nxtarg(0), &statb
) >= 0 &&
159 (statb
.st_mode
& S_IFMT
) != S_IFDIR
);
162 return(filtyp(nxtarg(0), S_IFREG
));
164 return(ftype(nxtarg(0), S_ISUID
));
166 return(ftype(nxtarg(0), S_ISGID
));
168 return(ftype(nxtarg(0), S_ISVTX
));
170 return(filtyp(nxtarg(0), S_IFIFO
));
171 if (eq(a
, "-h") || eq(a
, "-L"))
172 return(filtyp(nxtarg(0), S_IFLNK
));
174 return(fsizep(nxtarg(0)));
177 if (ap
>= ac
) /* no args */
179 else if (eq((a
= nxtarg(0)), "-a") || eq(a
, "-o"))
185 return(isatty(atoi((char *)a
)));
188 return(!eq(nxtarg(0), ""));
190 return(eq(nxtarg(0), ""));
196 if (eq(p2
, "-a") || eq(p2
, "-o"))
202 return(eq(nxtarg(0), a
));
204 return(!eq(nxtarg(0), a
));
205 ll_1
= strtoll((char *)a
, NULL
, 10);
206 ll_2
= strtoll((char *)nxtarg(0), NULL
, 10);
208 return (ll_1
== ll_2
);
210 return (ll_1
!= ll_2
);
212 return (ll_1
> ll_2
);
214 return (ll_1
< ll_2
);
216 return (ll_1
>= ll_2
);
218 return (ll_1
<= ll_2
);
220 bfailed((unsigned char *)btest
, badop
, p2
);
225 ftype(unsigned char *f
, int field
)
229 if (stat((char *)f
, &statb
) < 0)
231 if ((statb
.st_mode
& field
) == field
)
237 filtyp(unsigned char *f
, int field
)
240 int (*statf
)() = (field
== S_IFLNK
) ? lstat
: stat
;
242 if ((*statf
)(f
, &statb
) < 0)
244 if ((statb
.st_mode
& S_IFMT
) == field
)
252 fsizep(unsigned char *f
)
256 if (stat((char *)f
, &statb
) < 0)
258 return(statb
.st_size
> 0);