1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include "toys.h"
41
42void test_main(void)
43{
44 int id, not;
45 char *s, *err_fmt = "Bad flag '%s'";
46
47 toys.exitval = 2;
48 if (!strcmp("[", toys.which->name))
49 if (!strcmp("]", toys.optargs[--toys.optc])) error_exit("Missing ']'");
50 if (!strcmp("!", toys.optargs[0])) {
51 not = 1;
52 toys.optargs++;
53 toys.optc--;
54 }
55 if (!toys.optc) toys.exitval = 0;
56 else if (toys.optargs[0][0] == '-') {
57 id = stridx("bcdefghLpSsurwxznt", toys.optargs[0][1]);
58 if (id == -1 || toys.optargs[0][2]) error_exit(err_fmt, toys.optargs[0]);
59 if (id < 12) {
60 struct stat st;
61 int nolink;
62
63 toys.exitval = 1;
64 if (lstat(toys.optargs[1], &st) == -1) return;
65 nolink = !S_ISLNK(st.st_mode);
66 if (!nolink && (stat(toys.optargs[1], &st) == -1)) return;
67
68 if (id == 0) toys.exitval = !S_ISBLK(st.st_mode);
69 else if (id == 1) toys.exitval = !S_ISCHR(st.st_mode);
70 else if (id == 2) toys.exitval = !S_ISDIR(st.st_mode);
71 else if (id == 3) toys.exitval = 0;
72 else if (id == 4) toys.exitval = !S_ISREG(st.st_mode);
73 else if (id == 5) toys.exitval = !(st.st_mode & S_ISGID);
74 else if ((id == 6) || (id == 7)) toys.exitval = nolink;
75 else if (id == 8) toys.exitval = !S_ISFIFO(st.st_mode);
76 else if (id == 9) toys.exitval = !S_ISSOCK(st.st_mode);
77 else if (id == 10) toys.exitval = st.st_size == 0;
78 else toys.exitval = !(st.st_mode & S_ISUID);
79 }
80 else if (id < 15)
81 toys.exitval = access(toys.optargs[1], 1 << (id - 12)) == -1;
82 else if (id < 17)
83 toys.exitval = toys.optargs[1] && !*toys.optargs[1] ^ (id - 15);
84 else {
85 struct termios termios;
86 toys.exitval = tcgetattr(atoi(toys.optargs[1]), &termios) == -1;
87 }
88 }
89 else if (toys.optc == 1) toys.exitval = *toys.optargs[0] == 0;
90 else if (toys.optc == 3) {
91 if (*toys.optargs[1] == '-') {
92 long a = atol(toys.optargs[0]), b = atol(toys.optargs[2]);
93
94 s = toys.optargs[1] + 1;
95 if (!strcmp("eq", s)) toys.exitval = a != b;
96 else if (!strcmp("ne", s)) toys.exitval = a == b;
97 else if (!strcmp("gt", s)) toys.exitval = a < b;
98 else if (!strcmp("ge", s)) toys.exitval = a <= b;
99 else if (!strcmp("lt", s)) toys.exitval = a > b;
100 else if (!strcmp("le", s)) toys.exitval = a >= b;
101 else error_exit(err_fmt, toys.optargs[1]);
102 }
103 else {
104 int result = strcmp(toys.optargs[0], toys.optargs[2]);
105
106 s = toys.optargs[1];
107 if (!strcmp("=", s)) toys.exitval = !!result;
108 else if (!strcmp("!=", s)) toys.exitval = !result;
109 else error_exit(err_fmt, toys.optargs[1]);
110 }
111 }
112 toys.exitval ^= not;
113 return;
114}
115