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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/**
* @file test.c
* @brief The `test` and `[` builtins.
*/
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "exec.h"
/**
* @brief State of one `test` evaluation: the arguments and the read position.
*/
typedef struct test_state {
char **argv;
int pos;
int argc;
bool error;
} test_state;
static bool parse_or(test_state *t);
/**
* @brief Number of arguments not yet consumed.
*/
static int remaining(const test_state *t) {
return t->argc - t->pos;
}
/**
* @brief Is this a binary operator of `test` (`=`, `-eq`, `-nt` ...)?
*/
static bool is_binary_op(const char *s) {
static const char *const ops[] = {"=", "==", "!=", "<", ">",
"-eq", "-ne", "-lt", "-le", "-gt",
"-ge", "-nt", "-ot", "-ef"};
for (size_t i = 0; i < ARRAY_LEN(ops); i++) {
if (!strcmp(s, ops[i]))
return true;
}
return false;
}
/**
* @brief Is this a unary operator of `test` (`-f`, `-z` ...)?
*/
static bool is_unary_op(const char *s) {
return s[0] == '-' && s[1] && !s[2] &&
strchr("bcdefghkLnprsStuwxzOG", s[1]);
}
/**
* @brief Convert an operand of a numeric comparison.
* @param t State. `error` is set if `s` is not an integer.
* @param s The operand.
* @return Its value.
*/
static intmax_t to_int(test_state *t, const char *s) {
char *end;
errno = 0;
intmax_t v = strtoimax(s, &end, 10);
while (*end == ' ' || *end == '\t')
end++;
if (end == s || *end || errno) {
sh_warn("test: %s: integer expected", s);
t->error = true;
}
return v;
}
/**
* @brief Evaluate a unary test such as `-f file` or `-n string`.
* @param op The operator's letter.
* @param arg Its operand.
*/
bool test_unary(char op, const char *arg) {
struct stat st;
switch (op) {
case 'n':
return *arg != '\0';
case 'z':
return *arg == '\0';
case 't': {
long fd;
return parse_long(arg, &fd) && isatty((int)fd);
}
case 'r':
return access(arg, R_OK) == 0;
case 'w':
return access(arg, W_OK) == 0;
case 'x':
return access(arg, X_OK) == 0;
case 'L':
case 'h':
return lstat(arg, &st) == 0 && S_ISLNK(st.st_mode);
default:
break;
}
if (stat(arg, &st) != 0)
return false;
switch (op) {
case 'e':
return true;
case 'f':
return S_ISREG(st.st_mode);
case 'd':
return S_ISDIR(st.st_mode);
case 'b':
return S_ISBLK(st.st_mode);
case 'c':
return S_ISCHR(st.st_mode);
case 'p':
return S_ISFIFO(st.st_mode);
case 'S':
return S_ISSOCK(st.st_mode);
case 's':
return st.st_size > 0;
case 'g':
return (st.st_mode & S_ISGID) != 0;
case 'u':
return (st.st_mode & S_ISUID) != 0;
case 'k':
return (st.st_mode & S_ISVTX) != 0;
case 'O':
return st.st_uid == geteuid();
case 'G':
return st.st_gid == getegid();
default:
return false;
}
}
/**
* @brief Compare two files: `-nt`, `-ot` or `-ef`.
*
* As in POSIX.1-2024, an existing file is newer than a missing one.
*
* @param a First file.
* @param op `n` (newer than), `o` (older than) or `e` (same file).
* @param b Second file.
*/
bool test_files(const char *a, char op, const char *b) {
struct stat sa;
struct stat sb;
bool ha = stat(a, &sa) == 0;
bool hb = stat(b, &sb) == 0;
if (op == 'e')
return ha && hb && sa.st_dev == sb.st_dev && sa.st_ino == sb.st_ino;
if (op == 'n')
return ha && (!hb || sa.st_mtime > sb.st_mtime);
return hb && (!ha || sa.st_mtime < sb.st_mtime);
}
/**
* @brief Evaluate a binary test.
* @param t State, for error reporting.
* @param a Left operand.
* @param op The operator.
* @param b Right operand.
*/
static bool eval_binary(test_state *t, const char *a, const char *op,
const char *b) {
if (op[0] != '-') {
int c = strcmp(a, b);
if (op[0] == '=')
return c == 0;
if (op[0] == '!')
return c != 0;
return op[0] == '<' ? c < 0 : c > 0;
}
if (!strcmp(op, "-nt") || !strcmp(op, "-ot") || !strcmp(op, "-ef"))
return test_files(a, op[1], b);
intmax_t x = to_int(t, a);
intmax_t y = to_int(t, b);
if (!strcmp(op, "-eq"))
return x == y;
if (!strcmp(op, "-ne"))
return x != y;
if (!strcmp(op, "-lt"))
return x < y;
if (!strcmp(op, "-le"))
return x <= y;
if (!strcmp(op, "-gt"))
return x > y;
return x >= y;
}
/**
* @brief Is the argument after the current one a binary operator that has both
* of its operands?
*/
static bool binary_next(const test_state *t) {
return remaining(t) >= 3 && is_binary_op(t->argv[t->pos + 1]);
}
/**
* @brief Parse one primary: `( expr )`, a unary or binary test, or a bare
* string (true if not empty).
*/
static bool parse_primary(test_state *t) {
if (remaining(t) == 0) {
sh_warn("test: argument expected");
t->error = true;
return false;
}
const char *a = t->argv[t->pos];
if (binary_next(t)) {
const char *op = t->argv[t->pos + 1];
const char *b = t->argv[t->pos + 2];
t->pos += 3;
return eval_binary(t, a, op, b);
}
if (!strcmp(a, "(") && remaining(t) >= 3) {
t->pos++;
bool v = parse_or(t);
if (remaining(t) == 0 || strcmp(t->argv[t->pos], ")")) {
sh_warn("test: missing )");
t->error = true;
return false;
}
t->pos++;
return v;
}
if (is_unary_op(a) && remaining(t) >= 2) {
t->pos += 2;
return test_unary(a[1], t->argv[t->pos - 1]);
}
t->pos++;
return *a != '\0';
}
/**
* @brief Parse `! expr`.
*/
static bool parse_not(test_state *t) {
if (remaining(t) >= 2 && !strcmp(t->argv[t->pos], "!") && !binary_next(t)) {
t->pos++;
return !parse_not(t);
}
return parse_primary(t);
}
/**
* @brief Parse `expr -a expr`.
*/
static bool parse_and(test_state *t) {
bool v = parse_not(t);
while (remaining(t) > 0 && !strcmp(t->argv[t->pos], "-a")) {
t->pos++;
bool rhs = parse_not(t);
v = v && rhs;
}
return v;
}
/**
* @brief Parse `expr -o expr`.
*/
static bool parse_or(test_state *t) {
bool v = parse_and(t);
while (remaining(t) > 0 && !strcmp(t->argv[t->pos], "-o")) {
t->pos++;
bool rhs = parse_and(t);
v = v || rhs;
}
return v;
}
/**
* @brief The `test` and `[` builtins.
*
* With up to four arguments the POSIX rules by argument count are
* followed exactly, so that operands that look like operators are not
* misread. Longer expressions use the recursive parser.
*
* @return 0 if the expression is true, 1 if false, 2 on a syntax error.
*/
int builtin_test(int argc, char **argv) {
if (!strcmp(argv[0], "[")) {
if (argc < 2 || strcmp(argv[argc - 1], "]")) {
sh_warn("[: missing ]");
return 2;
}
argc--;
}
if (argc == 1)
return 1;
test_state t = {.argv = argv, .pos = 1, .argc = argc};
bool v = parse_or(&t);
if (!t.error && remaining(&t) > 0) {
sh_warn("test: %s: unexpected operator", t.argv[t.pos]);
t.error = true;
}
if (t.error)
return 2;
return v ? 0 : 1;
}
|