jcs's openbsd hax
openbsd
1/* $OpenBSD: c_test.c,v 1.29 2025/03/24 22:19:42 millert Exp $ */
2
3/*
4 * test(1); version 7-like -- author Erik Baalbergen
5 * modified by Eric Gisin to be used as built-in.
6 * modified by Arnold Robbins to add SVR3 compatibility
7 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
8 * modified by Michael Rendell to add Korn's [[ .. ]] expressions.
9 * modified by J.T. Conklin to add POSIX compatibility.
10 */
11
12#include <sys/stat.h>
13
14#include <string.h>
15#include <unistd.h>
16
17#include "sh.h"
18#include "c_test.h"
19
20/* test(1) accepts the following grammar:
21 oexpr ::= aexpr | aexpr "-o" oexpr ;
22 aexpr ::= nexpr | nexpr "-a" aexpr ;
23 nexpr ::= primary | "!" nexpr ;
24 primary ::= unary-operator operand
25 | operand binary-operator operand
26 | operand
27 | "(" oexpr ")"
28 ;
29
30 unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
31 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
32 "-L"|"-h"|"-S"|"-H";
33
34 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
35 "-nt"|"-ot"|"-ef"|"<"|">"
36 ;
37 operand ::= <any thing>
38*/
39
40#define T_ERR_EXIT 2 /* POSIX says > 1 for errors */
41
42struct t_op {
43 char op_text[4];
44 Test_op op_num;
45};
46static const struct t_op u_ops [] = {
47 {"-a", TO_FILAXST },
48 {"-b", TO_FILBDEV },
49 {"-c", TO_FILCDEV },
50 {"-d", TO_FILID },
51 {"-e", TO_FILEXST },
52 {"-f", TO_FILREG },
53 {"-G", TO_FILGID },
54 {"-g", TO_FILSETG },
55 {"-h", TO_FILSYM },
56 {"-H", TO_FILCDF },
57 {"-k", TO_FILSTCK },
58 {"-L", TO_FILSYM },
59 {"-n", TO_STNZE },
60 {"-O", TO_FILUID },
61 {"-o", TO_OPTION },
62 {"-p", TO_FILFIFO },
63 {"-r", TO_FILRD },
64 {"-s", TO_FILGZ },
65 {"-S", TO_FILSOCK },
66 {"-t", TO_FILTT },
67 {"-u", TO_FILSETU },
68 {"-w", TO_FILWR },
69 {"-x", TO_FILEX },
70 {"-z", TO_STZER },
71 {"", TO_NONOP }
72};
73static const struct t_op b_ops [] = {
74 {"=", TO_STEQL },
75 {"==", TO_STEQL },
76 {"!=", TO_STNEQ },
77 {"<", TO_STLT },
78 {">", TO_STGT },
79 {"-eq", TO_INTEQ },
80 {"-ne", TO_INTNE },
81 {"-gt", TO_INTGT },
82 {"-ge", TO_INTGE },
83 {"-lt", TO_INTLT },
84 {"-le", TO_INTLE },
85 {"-ef", TO_FILEQ },
86 {"-nt", TO_FILNT },
87 {"-ot", TO_FILOT },
88 {"", TO_NONOP }
89};
90
91static int test_eaccess(const char *, int);
92static int test_oexpr(Test_env *, int);
93static int test_aexpr(Test_env *, int);
94static int test_nexpr(Test_env *, int);
95static int test_primary(Test_env *, int);
96static int ptest_isa(Test_env *, Test_meta);
97static const char *ptest_getopnd(Test_env *, Test_op, int);
98static int ptest_eval(Test_env *, Test_op, const char *,
99 const char *, int);
100static void ptest_error(Test_env *, int, const char *);
101
102int
103c_test(char **wp)
104{
105 int argc;
106 int res;
107 Test_env te;
108
109 te.flags = 0;
110 te.isa = ptest_isa;
111 te.getopnd = ptest_getopnd;
112 te.eval = ptest_eval;
113 te.error = ptest_error;
114
115 for (argc = 0; wp[argc]; argc++)
116 ;
117
118 if (strcmp(wp[0], "[") == 0) {
119 if (strcmp(wp[--argc], "]") != 0) {
120 bi_errorf("missing ]");
121 return T_ERR_EXIT;
122 }
123 }
124
125 te.pos.wp = wp + 1;
126 te.wp_end = wp + argc;
127
128 /*
129 * Handle the special cases from POSIX.2, section 4.62.4.
130 * Implementation of all the rules isn't necessary since
131 * our parser does the right thing for the omitted steps.
132 */
133 if (argc <= 5) {
134 char **owp = wp;
135 int invert = 0;
136 Test_op op;
137 const char *opnd1, *opnd2;
138
139 while (--argc >= 0) {
140 if ((*te.isa)(&te, TM_END))
141 return !0;
142 if (argc == 3) {
143 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
144 if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
145 opnd2 = (*te.getopnd)(&te, op, 1);
146 res = (*te.eval)(&te, op, opnd1,
147 opnd2, 1);
148 if (te.flags & TEF_ERROR)
149 return T_ERR_EXIT;
150 if (invert & 1)
151 res = !res;
152 return !res;
153 }
154 /* back up to opnd1 */
155 te.pos.wp--;
156 }
157 if (argc == 1) {
158 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
159 res = (*te.eval)(&te, TO_STNZE, opnd1,
160 NULL, 1);
161 if (invert & 1)
162 res = !res;
163 return !res;
164 }
165 if ((*te.isa)(&te, TM_NOT)) {
166 invert++;
167 } else
168 break;
169 }
170 te.pos.wp = owp + 1;
171 }
172
173 return test_parse(&te);
174}
175
176/*
177 * Generic test routines.
178 */
179
180Test_op
181test_isop(Test_env *te, Test_meta meta, const char *s)
182{
183 char sc1;
184 const struct t_op *otab;
185
186 otab = meta == TM_UNOP ? u_ops : b_ops;
187 if (*s) {
188 sc1 = s[1];
189 for (; otab->op_text[0]; otab++)
190 if (sc1 == otab->op_text[1] &&
191 strcmp(s, otab->op_text) == 0)
192 return otab->op_num;
193 }
194 return TO_NONOP;
195}
196
197int
198test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
199 int do_eval)
200{
201 int res;
202 int not;
203 struct stat b1, b2;
204
205 if (!do_eval)
206 return 0;
207
208 switch ((int) op) {
209 /*
210 * Unary Operators
211 */
212 case TO_STNZE: /* -n */
213 return *opnd1 != '\0';
214 case TO_STZER: /* -z */
215 return *opnd1 == '\0';
216 case TO_OPTION: /* -o */
217 if ((not = *opnd1 == '!'))
218 opnd1++;
219 if ((res = option(opnd1)) < 0)
220 res = 0;
221 else {
222 res = Flag(res);
223 if (not)
224 res = !res;
225 }
226 return res;
227 case TO_FILRD: /* -r */
228 return test_eaccess(opnd1, R_OK) == 0;
229 case TO_FILWR: /* -w */
230 return test_eaccess(opnd1, W_OK) == 0;
231 case TO_FILEX: /* -x */
232 return test_eaccess(opnd1, X_OK) == 0;
233 case TO_FILAXST: /* -a */
234 return stat(opnd1, &b1) == 0;
235 case TO_FILEXST: /* -e */
236 /* at&t ksh does not appear to do the /dev/fd/ thing for
237 * this (unless the os itself handles it)
238 */
239 return stat(opnd1, &b1) == 0;
240 case TO_FILREG: /* -r */
241 return stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
242 case TO_FILID: /* -d */
243 return stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
244 case TO_FILCDEV: /* -c */
245 return stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
246 case TO_FILBDEV: /* -b */
247 return stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
248 case TO_FILFIFO: /* -p */
249 return stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
250 case TO_FILSYM: /* -h -L */
251 return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
252 case TO_FILSOCK: /* -S */
253 return stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
254 case TO_FILCDF:/* -H HP context dependent files (directories) */
255 return 0;
256 case TO_FILSETU: /* -u */
257 return stat(opnd1, &b1) == 0 &&
258 (b1.st_mode & S_ISUID) == S_ISUID;
259 case TO_FILSETG: /* -g */
260 return stat(opnd1, &b1) == 0 &&
261 (b1.st_mode & S_ISGID) == S_ISGID;
262 case TO_FILSTCK: /* -k */
263 return stat(opnd1, &b1) == 0 &&
264 (b1.st_mode & S_ISVTX) == S_ISVTX;
265 case TO_FILGZ: /* -s */
266 return stat(opnd1, &b1) == 0 && b1.st_size > 0L;
267 case TO_FILTT: /* -t */
268 if (!bi_getn(opnd1, &res)) {
269 te->flags |= TEF_ERROR;
270 return 0;
271 }
272 return isatty(res);
273 case TO_FILUID: /* -O */
274 return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
275 case TO_FILGID: /* -G */
276 return stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
277 /*
278 * Binary Operators
279 */
280 case TO_STEQL: /* = */
281 if (te->flags & TEF_DBRACKET)
282 return gmatch(opnd1, opnd2, false);
283 return strcmp(opnd1, opnd2) == 0;
284 case TO_STNEQ: /* != */
285 if (te->flags & TEF_DBRACKET)
286 return !gmatch(opnd1, opnd2, false);
287 return strcmp(opnd1, opnd2) != 0;
288 case TO_STLT: /* < */
289 return strcmp(opnd1, opnd2) < 0;
290 case TO_STGT: /* > */
291 return strcmp(opnd1, opnd2) > 0;
292 case TO_INTEQ: /* -eq */
293 case TO_INTNE: /* -ne */
294 case TO_INTGE: /* -ge */
295 case TO_INTGT: /* -gt */
296 case TO_INTLE: /* -le */
297 case TO_INTLT: /* -lt */
298 {
299 int64_t v1, v2;
300
301 if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR, false) ||
302 !evaluate(opnd2, &v2, KSH_RETURN_ERROR, false)) {
303 /* error already printed.. */
304 te->flags |= TEF_ERROR;
305 return 1;
306 }
307 switch ((int) op) {
308 case TO_INTEQ:
309 return v1 == v2;
310 case TO_INTNE:
311 return v1 != v2;
312 case TO_INTGE:
313 return v1 >= v2;
314 case TO_INTGT:
315 return v1 > v2;
316 case TO_INTLE:
317 return v1 <= v2;
318 case TO_INTLT:
319 return v1 < v2;
320 }
321 }
322 case TO_FILNT: /* -nt */
323 {
324 int s2;
325 /* ksh88/ksh93 succeed if file2 can't be stated
326 * (subtly different from `does not exist').
327 */
328 return stat(opnd1, &b1) == 0 &&
329 (((s2 = stat(opnd2, &b2)) == 0 &&
330 timespeccmp(&b1.st_mtim, &b2.st_mtim, >)) ||
331 s2 != 0);
332 }
333 case TO_FILOT: /* -ot */
334 {
335 int s1;
336 /* ksh88/ksh93 succeed if file1 can't be stated
337 * (subtly different from `does not exist').
338 */
339 return stat(opnd2, &b2) == 0 &&
340 (((s1 = stat(opnd1, &b1)) == 0 &&
341 timespeccmp(&b1.st_mtim, &b2.st_mtim, <)) ||
342 s1 != 0);
343 }
344 case TO_FILEQ: /* -ef */
345 return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 &&
346 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
347 }
348 (*te->error)(te, 0, "internal error: unknown op");
349 return 1;
350}
351
352/* Routine to deal with X_OK on non-directories when running as root.
353 */
354static int
355test_eaccess(const char *path, int amode)
356{
357 int res;
358
359 res = access(path, amode);
360 /*
361 * On most (all?) unixes, access() says everything is executable for
362 * root - avoid this on files by using stat().
363 */
364 if (res == 0 && ksheuid == 0 && (amode & X_OK)) {
365 struct stat statb;
366
367 if (stat(path, &statb) == -1)
368 res = -1;
369 else if (S_ISDIR(statb.st_mode))
370 res = 0;
371 else
372 res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) ?
373 0 : -1;
374 }
375
376 return res;
377}
378
379int
380test_parse(Test_env *te)
381{
382 int res;
383
384 res = test_oexpr(te, 1);
385
386 if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
387 (*te->error)(te, 0, "unexpected operator/operand");
388
389 return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
390}
391
392static int
393test_oexpr(Test_env *te, int do_eval)
394{
395 int res;
396
397 res = test_aexpr(te, do_eval);
398 if (res)
399 do_eval = 0;
400 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
401 return test_oexpr(te, do_eval) || res;
402 return res;
403}
404
405static int
406test_aexpr(Test_env *te, int do_eval)
407{
408 int res;
409
410 res = test_nexpr(te, do_eval);
411 if (!res)
412 do_eval = 0;
413 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
414 return test_aexpr(te, do_eval) && res;
415 return res;
416}
417
418static int
419test_nexpr(Test_env *te, int do_eval)
420{
421 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
422 return !test_nexpr(te, do_eval);
423 return test_primary(te, do_eval);
424}
425
426static int
427test_primary(Test_env *te, int do_eval)
428{
429 const char *opnd1, *opnd2;
430 int res;
431 Test_op op;
432
433 if (te->flags & TEF_ERROR)
434 return 0;
435 if ((*te->isa)(te, TM_OPAREN)) {
436 res = test_oexpr(te, do_eval);
437 if (te->flags & TEF_ERROR)
438 return 0;
439 if (!(*te->isa)(te, TM_CPAREN)) {
440 (*te->error)(te, 0, "missing closing paren");
441 return 0;
442 }
443 return res;
444 }
445 /*
446 * Binary should have precedence over unary in this case
447 * so that something like test \( -f = -f \) is accepted
448 */
449 if ((te->flags & TEF_DBRACKET) || (&te->pos.wp[1] < te->wp_end &&
450 !test_isop(te, TM_BINOP, te->pos.wp[1]))) {
451 if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
452 /* unary expression */
453 opnd1 = (*te->getopnd)(te, op, do_eval);
454 if (!opnd1) {
455 (*te->error)(te, -1, "missing argument");
456 return 0;
457 }
458
459 return (*te->eval)(te, op, opnd1, NULL,
460 do_eval);
461 }
462 }
463 opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
464 if (!opnd1) {
465 (*te->error)(te, 0, "expression expected");
466 return 0;
467 }
468 if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
469 /* binary expression */
470 opnd2 = (*te->getopnd)(te, op, do_eval);
471 if (!opnd2) {
472 (*te->error)(te, -1, "missing second argument");
473 return 0;
474 }
475
476 return (*te->eval)(te, op, opnd1, opnd2, do_eval);
477 }
478 if (te->flags & TEF_DBRACKET) {
479 (*te->error)(te, -1, "missing expression operator");
480 return 0;
481 }
482 return (*te->eval)(te, TO_STNZE, opnd1, NULL, do_eval);
483}
484
485/*
486 * Plain test (test and [ .. ]) specific routines.
487 */
488
489/* Test if the current token is a whatever. Accepts the current token if
490 * it is. Returns 0 if it is not, non-zero if it is (in the case of
491 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
492 */
493static int
494ptest_isa(Test_env *te, Test_meta meta)
495{
496 /* Order important - indexed by Test_meta values */
497 static const char *const tokens[] = {
498 "-o", "-a", "!", "(", ")"
499 };
500 int ret;
501
502 if (te->pos.wp >= te->wp_end)
503 return meta == TM_END;
504
505 if (meta == TM_UNOP || meta == TM_BINOP)
506 ret = (int) test_isop(te, meta, *te->pos.wp);
507 else if (meta == TM_END)
508 ret = 0;
509 else
510 ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
511
512 /* Accept the token? */
513 if (ret)
514 te->pos.wp++;
515
516 return ret;
517}
518
519static const char *
520ptest_getopnd(Test_env *te, Test_op op, int do_eval)
521{
522 if (te->pos.wp >= te->wp_end)
523 return NULL;
524 return *te->pos.wp++;
525}
526
527static int
528ptest_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
529 int do_eval)
530{
531 return test_eval(te, op, opnd1, opnd2, do_eval);
532}
533
534static void
535ptest_error(Test_env *te, int offset, const char *msg)
536{
537 const char *op = te->pos.wp + offset >= te->wp_end ?
538 NULL : te->pos.wp[offset];
539
540 te->flags |= TEF_ERROR;
541 if (op)
542 bi_errorf("%s: %s", op, msg);
543 else
544 bi_errorf("%s", msg);
545}