2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
25 #include <sys/types.h>
29 #define EQ(a, b) ((strcmp(a, b) == 0))
31 static char *nxtarg(int mt
);
36 static int tio(char *a
, int f
);
37 static int ftype(char *f
, int field
);
38 static int filtyp(char *f
, int field
);
39 static int fsizep(char *f
);
40 static void synbad(char *s1
, char *s2
);
47 main(int argc
, char *argv
[])
51 ac
= argc
; av
= argv
; ap
= 1;
52 if (EQ(argv
[0], "[")) {
53 if (!EQ(argv
[--ac
], "]"))
54 synbad("] missing", "");
59 status
= (exp() ? 0 : 1);
61 synbad("too many arguments", "");
73 synbad("argument expected", "");
90 synbad("syntax error", "");
104 if ((p2
!= 0) && EQ(p2
, "-a"))
113 if (EQ(nxtarg(0), "!"))
130 if (!EQ(nxtarg(0), ")")) synbad(") expected", "");
135 if ((p2
== 0) || (!EQ(p2
, "=") && !EQ(p2
, "!="))) {
137 return (tio(nxtarg(0), 4));
140 return (tio(nxtarg(0), 2));
143 return (tio(nxtarg(0), 1));
146 return (filtyp(nxtarg(0), S_IFDIR
));
149 return (filtyp(nxtarg(0), S_IFCHR
));
152 return (filtyp(nxtarg(0), S_IFBLK
));
157 return (stat(nxtarg(0), &statb
) >= 0 &&
158 (statb
.st_mode
& S_IFMT
) != S_IFDIR
);
162 return (filtyp(nxtarg(0), S_IFLNK
));
165 return (ftype(nxtarg(0), S_ISUID
));
168 return (ftype(nxtarg(0), S_ISGID
));
171 return (ftype(nxtarg(0), S_ISVTX
));
175 return (filtyp(nxtarg(0), S_IFIFO
));
177 return (nxtarg(0), 0);
181 return (fsizep(nxtarg(0)));
186 else if (EQ((a
= nxtarg(0)), "-a") || EQ(a
, "-o")) {
190 return (isatty(atoi(a
)));
193 return (!EQ(nxtarg(0), ""));
195 return (EQ(nxtarg(0), ""));
201 if (EQ(p2
, "-a") || EQ(p2
, "-o")) {
206 return (EQ(nxtarg(0), a
));
209 return (!EQ(nxtarg(0), a
));
212 int2
= atoi(nxtarg(0));
214 return (int1
== int2
);
216 return (int1
!= int2
);
218 return (int1
> int2
);
220 return (int1
< int2
);
222 return (int1
>= int2
);
224 return (int1
<= int2
);
226 synbad("unknown operator ", p2
);
234 if (access(a
, f
) == 0)
241 ftype(char *f
, int field
)
245 if (stat(f
, &statb
) < 0)
247 if ((statb
.st_mode
& field
) == field
)
253 filtyp(char *f
, int field
)
257 if (field
== S_IFLNK
) {
258 if (lstat(f
, &statb
) < 0)
261 if (stat(f
, &statb
) < 0)
264 if ((statb
.st_mode
& S_IFMT
) == field
)
275 if (stat(f
, &statb
) < 0)
277 return (statb
.st_size
> 0);
281 synbad(char *s1
, char *s2
)
283 (void) write(2, "test: ", 6);
284 (void) write(2, s1
, strlen(s1
));
285 (void) write(2, s2
, strlen(s2
));
286 (void) write(2, "\n", 1);