GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ksh/c_test.c Lines: 125 200 62.5 %
Date: 2016-12-06 Branches: 126 265 47.5 %

Line Branch Exec Source
1
/*	$OpenBSD: c_test.c,v 1.23 2015/12/14 13:59:42 tb 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
			    "<"|">"	# rules used for [[ .. ]] expressions
37
			    ;
38
	operand ::= <any thing>
39
*/
40
41
#define T_ERR_EXIT	2	/* POSIX says > 1 for errors */
42
43
struct t_op {
44
	char	op_text[4];
45
	Test_op	op_num;
46
};
47
static const struct t_op u_ops [] = {
48
	{"-a",	TO_FILAXST },
49
	{"-b",	TO_FILBDEV },
50
	{"-c",	TO_FILCDEV },
51
	{"-d",	TO_FILID },
52
	{"-e",	TO_FILEXST },
53
	{"-f",	TO_FILREG },
54
	{"-G",	TO_FILGID },
55
	{"-g",	TO_FILSETG },
56
	{"-h",	TO_FILSYM },
57
	{"-H",	TO_FILCDF },
58
	{"-k",	TO_FILSTCK },
59
	{"-L",	TO_FILSYM },
60
	{"-n",	TO_STNZE },
61
	{"-O",	TO_FILUID },
62
	{"-o",	TO_OPTION },
63
	{"-p",	TO_FILFIFO },
64
	{"-r",	TO_FILRD },
65
	{"-s",	TO_FILGZ },
66
	{"-S",	TO_FILSOCK },
67
	{"-t",	TO_FILTT },
68
	{"-u",	TO_FILSETU },
69
	{"-w",	TO_FILWR },
70
	{"-x",	TO_FILEX },
71
	{"-z",	TO_STZER },
72
	{"",	TO_NONOP }
73
};
74
static const struct t_op b_ops [] = {
75
	{"=",	TO_STEQL },
76
	{"==",	TO_STEQL },
77
	{"!=",	TO_STNEQ },
78
	{"<",	TO_STLT },
79
	{">",	TO_STGT },
80
	{"-eq",	TO_INTEQ },
81
	{"-ne",	TO_INTNE },
82
	{"-gt",	TO_INTGT },
83
	{"-ge",	TO_INTGE },
84
	{"-lt",	TO_INTLT },
85
	{"-le",	TO_INTLE },
86
	{"-ef",	TO_FILEQ },
87
	{"-nt",	TO_FILNT },
88
	{"-ot",	TO_FILOT },
89
	{"",	TO_NONOP }
90
};
91
92
static int	test_stat(const char *, struct stat *);
93
static int	test_eaccess(const char *, int);
94
static int	test_oexpr(Test_env *, int);
95
static int	test_aexpr(Test_env *, int);
96
static int	test_nexpr(Test_env *, int);
97
static int	test_primary(Test_env *, int);
98
static int	ptest_isa(Test_env *, Test_meta);
99
static const char *ptest_getopnd(Test_env *, Test_op, int);
100
static int	ptest_eval(Test_env *, Test_op, const char *,
101
		    const char *, int);
102
static void	ptest_error(Test_env *, int, const char *);
103
104
int
105
c_test(char **wp)
106
12542
{
107
	int argc;
108
	int res;
109
	Test_env te;
110
111
12542
	te.flags = 0;
112
12542
	te.isa = ptest_isa;
113
12542
	te.getopnd = ptest_getopnd;
114
12542
	te.eval = ptest_eval;
115
12542
	te.error = ptest_error;
116
117
12542
	for (argc = 0; wp[argc]; argc++)
118
		;
119
120
12542
	if (strcmp(wp[0], "[") == 0) {
121
7411
		if (strcmp(wp[--argc], "]") != 0) {
122
			bi_errorf("missing ]");
123
			return T_ERR_EXIT;
124
		}
125
	}
126
127
12542
	te.pos.wp = wp + 1;
128
12542
	te.wp_end = wp + argc;
129
130
	/*
131
	 * Handle the special cases from POSIX.2, section 4.62.4.
132
	 * Implementation of all the rules isn't necessary since
133
	 * our parser does the right thing for the omitted steps.
134
	 */
135
12542
	if (argc <= 5) {
136
11142
		char **owp = wp;
137
11142
		int invert = 0;
138
		Test_op	op;
139
		const char *opnd1, *opnd2;
140
141
22308
		while (--argc >= 0) {
142
11166
			if ((*te.isa)(&te, TM_END))
143
				return !0;
144
11166
			if (argc == 3) {
145
6957
				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
146
6957
				if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) {
147
6933
					opnd2 = (*te.getopnd)(&te, op, 1);
148
6933
					res = (*te.eval)(&te, op, opnd1,
149
					    opnd2, 1);
150
6933
					if (te.flags & TEF_ERROR)
151
						return T_ERR_EXIT;
152
6933
					if (invert & 1)
153
						res = !res;
154
6933
					return !res;
155
				}
156
				/* back up to opnd1 */
157
24
				te.pos.wp--;
158
			}
159
4233
			if (argc == 1) {
160
				opnd1 = (*te.getopnd)(&te, TO_NONOP, 1);
161
				/* Historically, -t by itself test if fd 1
162
				 * is a file descriptor, but POSIX says its
163
				 * a string test...
164
				 */
165
				if (!Flag(FPOSIX) && strcmp(opnd1, "-t") == 0)
166
				    break;
167
				res = (*te.eval)(&te, TO_STNZE, opnd1,
168
				    NULL, 1);
169
				if (invert & 1)
170
					res = !res;
171
				return !res;
172
			}
173
4233
			if ((*te.isa)(&te, TM_NOT)) {
174
24
				invert++;
175
			} else
176
4209
				break;
177
		}
178
4209
		te.pos.wp = owp + 1;
179
	}
180
181
5609
	return test_parse(&te);
182
}
183
184
/*
185
 * Generic test routines.
186
 */
187
188
Test_op
189
test_isop(Test_env *te, Test_meta meta, const char *s)
190
20981
{
191
	char sc1;
192
	const struct t_op *otab;
193
194
20981
	otab = meta == TM_UNOP ? u_ops : b_ops;
195
20981
	if (*s) {
196
20479
		sc1 = s[1];
197
124476
		for (; otab->op_text[0]; otab++)
198


120665
			if (sc1 == otab->op_text[1] &&
199
			    strcmp(s, otab->op_text) == 0 &&
200
			    ((te->flags & TEF_DBRACKET) ||
201
			    (otab->op_num != TO_STLT && otab->op_num != TO_STGT)))
202
16668
				return otab->op_num;
203
	}
204
4313
	return TO_NONOP;
205
}
206
207
int
208
test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
209
    int do_eval)
210
13945
{
211
	int res;
212
	int not;
213
	struct stat b1, b2;
214
215
13945
	if (!do_eval)
216
513
		return 0;
217
218








13432
	switch ((int) op) {
219
	/*
220
	 * Unary Operators
221
	 */
222
	case TO_STNZE: /* -n */
223
212
		return *opnd1 != '\0';
224
	case TO_STZER: /* -z */
225
570
		return *opnd1 == '\0';
226
	case TO_OPTION: /* -o */
227
		if ((not = *opnd1 == '!'))
228
			opnd1++;
229
		if ((res = option(opnd1)) < 0)
230
			res = 0;
231
		else {
232
			res = Flag(res);
233
			if (not)
234
				res = !res;
235
		}
236
		return res;
237
	case TO_FILRD: /* -r */
238
13
		return test_eaccess(opnd1, R_OK) == 0;
239
	case TO_FILWR: /* -w */
240
2
		return test_eaccess(opnd1, W_OK) == 0;
241
	case TO_FILEX: /* -x */
242
116
		return test_eaccess(opnd1, X_OK) == 0;
243
	case TO_FILAXST: /* -a */
244
		return test_stat(opnd1, &b1) == 0;
245
	case TO_FILEXST: /* -e */
246
		/* at&t ksh does not appear to do the /dev/fd/ thing for
247
		 * this (unless the os itself handles it)
248
		 */
249
1325
		return stat(opnd1, &b1) == 0;
250
	case TO_FILREG: /* -r */
251

210
		return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode);
252
	case TO_FILID: /* -d */
253

1431
		return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode);
254
	case TO_FILCDEV: /* -c */
255
		return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode);
256
	case TO_FILBDEV: /* -b */
257
		return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode);
258
	case TO_FILFIFO: /* -p */
259
		return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode);
260
	case TO_FILSYM: /* -h -L */
261

4
		return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode);
262
	case TO_FILSOCK: /* -S */
263
		return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode);
264
	case TO_FILCDF:/* -H HP context dependent files (directories) */
265
		return 0;
266
	case TO_FILSETU: /* -u */
267
		return test_stat(opnd1, &b1) == 0 &&
268
		    (b1.st_mode & S_ISUID) == S_ISUID;
269
	case TO_FILSETG: /* -g */
270
		return test_stat(opnd1, &b1) == 0 &&
271
		    (b1.st_mode & S_ISGID) == S_ISGID;
272
	case TO_FILSTCK: /* -k */
273
		return test_stat(opnd1, &b1) == 0 &&
274
		    (b1.st_mode & S_ISVTX) == S_ISVTX;
275
	case TO_FILGZ: /* -s */
276

367
		return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L;
277
	case TO_FILTT: /* -t */
278
		if (opnd1 && !bi_getn(opnd1, &res)) {
279
			te->flags |= TEF_ERROR;
280
			res = 0;
281
		} else {
282
			/* generate error if in FPOSIX mode? */
283
			res = isatty(opnd1 ? res : 0);
284
		}
285
		return res;
286
	case TO_FILUID: /* -O */
287
		return test_stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid;
288
	case TO_FILGID: /* -G */
289
		return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid();
290
	/*
291
	 * Binary Operators
292
	 */
293
	case TO_STEQL: /* = */
294
3964
		if (te->flags & TEF_DBRACKET)
295
			return gmatch(opnd1, opnd2, false);
296
3964
		return strcmp(opnd1, opnd2) == 0;
297
	case TO_STNEQ: /* != */
298
5098
		if (te->flags & TEF_DBRACKET)
299
			return !gmatch(opnd1, opnd2, false);
300
5098
		return strcmp(opnd1, opnd2) != 0;
301
	case TO_STLT: /* < */
302
		return strcmp(opnd1, opnd2) < 0;
303
	case TO_STGT: /* > */
304
		return strcmp(opnd1, opnd2) > 0;
305
	case TO_INTEQ: /* -eq */
306
	case TO_INTNE: /* -ne */
307
	case TO_INTGE: /* -ge */
308
	case TO_INTGT: /* -gt */
309
	case TO_INTLE: /* -le */
310
	case TO_INTLT: /* -lt */
311
		{
312
			long v1, v2;
313
314

116
			if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR, false) ||
315
			    !evaluate(opnd2, &v2, KSH_RETURN_ERROR, false)) {
316
				/* error already printed.. */
317
				te->flags |= TEF_ERROR;
318
				return 1;
319
			}
320

116
			switch ((int) op) {
321
			case TO_INTEQ:
322
28
				return v1 == v2;
323
			case TO_INTNE:
324
				return v1 != v2;
325
			case TO_INTGE:
326
				return v1 >= v2;
327
			case TO_INTGT:
328
88
				return v1 > v2;
329
			case TO_INTLE:
330
				return v1 <= v2;
331
			case TO_INTLT:
332
				return v1 < v2;
333
			}
334
		}
335
	case TO_FILNT: /* -nt */
336
		{
337
			int s2;
338
			/* ksh88/ksh93 succeed if file2 can't be stated
339
			 * (subtly different from `does not exist').
340
			 */
341


2
			return stat(opnd1, &b1) == 0 &&
342
			    (((s2 = stat(opnd2, &b2)) == 0 &&
343
			    b1.st_mtime > b2.st_mtime) || s2 < 0);
344
		}
345
	case TO_FILOT: /* -ot */
346
		{
347
			int s1;
348
			/* ksh88/ksh93 succeed if file1 can't be stated
349
			 * (subtly different from `does not exist').
350
			 */
351


2
			return stat(opnd2, &b2) == 0 &&
352
			    (((s1 = stat(opnd1, &b1)) == 0 &&
353
			    b1.st_mtime < b2.st_mtime) || s1 < 0);
354
		}
355
	case TO_FILEQ: /* -ef */
356
		return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 &&
357
		    b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
358
	}
359
	(*te->error)(te, 0, "internal error: unknown op");
360
	return 1;
361
}
362
363
/* Nasty kludge to handle Korn's bizarre /dev/fd hack */
364
static int
365
test_stat(const char *path, struct stat *statb)
366
2008
{
367
2008
	return stat(path, statb);
368
}
369
370
/* Routine to handle Korn's /dev/fd hack, and to deal with X_OK on
371
 * non-directories when running as root.
372
 */
373
static int
374
test_eaccess(const char *path, int mode)
375
131
{
376
	int res;
377
378
131
	res = access(path, mode);
379
	/*
380
	 * On most (all?) unixes, access() says everything is executable for
381
	 * root - avoid this on files by using stat().
382
	 */
383

131
	if (res == 0 && ksheuid == 0 && (mode & X_OK)) {
384
		struct stat statb;
385
386
116
		if (stat(path, &statb) < 0)
387
			res = -1;
388
116
		else if (S_ISDIR(statb.st_mode))
389
4
			res = 0;
390
		else
391
112
			res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) ?
392
			    0 : -1;
393
	}
394
395
131
	return res;
396
}
397
398
int
399
test_parse(Test_env *te)
400
5613
{
401
	int res;
402
403
5613
	res = test_oexpr(te, 1);
404
405

5613
	if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
406
		(*te->error)(te, 0, "unexpected operator/operand");
407
408
5613
	return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res;
409
}
410
411
static int
412
test_oexpr(Test_env *te, int do_eval)
413
6934
{
414
	int res;
415
416
6934
	res = test_aexpr(te, do_eval);
417
6934
	if (res)
418
2382
		do_eval = 0;
419

6934
	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
420

1321
		return test_oexpr(te, do_eval) || res;
421
5613
	return res;
422
}
423
424
static int
425
test_aexpr(Test_env *te, int do_eval)
426
7014
{
427
	int res;
428
429
7014
	res = test_nexpr(te, do_eval);
430
7014
	if (!res)
431
4593
		do_eval = 0;
432

7014
	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
433

80
		return test_aexpr(te, do_eval) && res;
434
6934
	return res;
435
}
436
437
static int
438
test_nexpr(Test_env *te, int do_eval)
439
7040
{
440

7040
	if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
441
26
		return !test_nexpr(te, do_eval);
442
7014
	return test_primary(te, do_eval);
443
}
444
445
static int
446
test_primary(Test_env *te, int do_eval)
447
7014
{
448
	const char *opnd1, *opnd2;
449
	int res;
450
	Test_op op;
451
452
7014
	if (te->flags & TEF_ERROR)
453
		return 0;
454
7014
	if ((*te->isa)(te, TM_OPAREN)) {
455
		res = test_oexpr(te, do_eval);
456
		if (te->flags & TEF_ERROR)
457
			return 0;
458
		if (!(*te->isa)(te, TM_CPAREN)) {
459
			(*te->error)(te, 0, "missing closing paren");
460
			return 0;
461
		}
462
		return res;
463
	}
464
	/*
465
	 * Binary should have precedence over unary in this case
466
	 * so that something like test \( -f = -f \) is accepted
467
	 */
468

7014
	if ((te->flags & TEF_DBRACKET) || (&te->pos.wp[1] < te->wp_end &&
469
	    !test_isop(te, TM_BINOP, te->pos.wp[1]))) {
470
4293
		if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) {
471
			/* unary expression */
472
4293
			opnd1 = (*te->getopnd)(te, op, do_eval);
473
4293
			if (!opnd1) {
474
				(*te->error)(te, -1, "missing argument");
475
				return 0;
476
			}
477
478
4293
			return (*te->eval)(te, op, opnd1, NULL,
479
			    do_eval);
480
		}
481
	}
482
2721
	opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
483
2721
	if (!opnd1) {
484
		(*te->error)(te, 0, "expression expected");
485
		return 0;
486
	}
487
2721
	if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) {
488
		/* binary expression */
489
2721
		opnd2 = (*te->getopnd)(te, op, do_eval);
490
2721
		if (!opnd2) {
491
			(*te->error)(te, -1, "missing second argument");
492
			return 0;
493
		}
494
495
2721
		return (*te->eval)(te, op, opnd1, opnd2, do_eval);
496
	}
497
	if (te->flags & TEF_DBRACKET) {
498
		(*te->error)(te, -1, "missing expression operator");
499
		return 0;
500
	}
501
	return (*te->eval)(te, TO_STNZE, opnd1, NULL, do_eval);
502
}
503
504
/*
505
 * Plain test (test and [ .. ]) specific routines.
506
 */
507
508
/* Test if the current token is a whatever.  Accepts the current token if
509
 * it is.  Returns 0 if it is not, non-zero if it is (in the case of
510
 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
511
 */
512
static int
513
ptest_isa(Test_env *te, Test_meta meta)
514
62961
{
515
	/* Order important - indexed by Test_meta values */
516
	static const char *const tokens[] = {
517
		"-o", "-a", "!", "(", ")"
518
	};
519
	int ret;
520
521
62961
	if (te->pos.wp >= te->wp_end)
522
16827
		return meta == TM_END;
523
524
46134
	if (meta == TM_UNOP || meta == TM_BINOP)
525
13967
		ret = (int) test_isop(te, meta, *te->pos.wp);
526
32167
	else if (meta == TM_END)
527
11166
		ret = 0;
528
	else
529
21001
		ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0;
530
531
	/* Accept the token? */
532
46134
	if (ret)
533
15394
		te->pos.wp++;
534
535
46134
	return ret;
536
}
537
538
static const char *
539
ptest_getopnd(Test_env *te, Test_op op, int do_eval)
540
23621
{
541
23621
	if (te->pos.wp >= te->wp_end)
542
		return op == TO_FILTT ? "1" : NULL;
543
23621
	return *te->pos.wp++;
544
}
545
546
static int
547
ptest_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
548
    int do_eval)
549
13943
{
550
13943
	return test_eval(te, op, opnd1, opnd2, do_eval);
551
}
552
553
static void
554
ptest_error(Test_env *te, int offset, const char *msg)
555
{
556
	const char *op = te->pos.wp + offset >= te->wp_end ?
557
	    NULL : te->pos.wp[offset];
558
559
	te->flags |= TEF_ERROR;
560
	if (op)
561
		bi_errorf("%s: %s", op, msg);
562
	else
563
		bi_errorf("%s", msg);
564
}