GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/test/test.c Lines: 109 159 68.6 %
Date: 2017-11-07 Branches: 79 139 56.8 %

Line Branch Exec Source
1
/*	$OpenBSD: test.c,v 1.18 2017/07/24 22:15:52 jca Exp $	*/
2
/*	$NetBSD: test.c,v 1.15 1995/03/21 07:04:06 cgd Exp $	*/
3
4
/*
5
 * test(1); version 7-like  --  author Erik Baalbergen
6
 * modified by Eric Gisin to be used as built-in.
7
 * modified by Arnold Robbins to add SVR3 compatibility
8
 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
9
 * modified by J.T. Conklin for NetBSD.
10
 *
11
 * This program is in the Public Domain.
12
 */
13
14
#include <sys/types.h>
15
#include <sys/stat.h>
16
#include <unistd.h>
17
#include <ctype.h>
18
#include <errno.h>
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <err.h>
23
24
/* test(1) accepts the following grammar:
25
	oexpr	::= aexpr | aexpr "-o" oexpr ;
26
	aexpr	::= nexpr | nexpr "-a" aexpr ;
27
	nexpr	::= primary | "!" primary
28
	primary	::= unary-operator operand
29
		| operand binary-operator operand
30
		| operand
31
		| "(" oexpr ")"
32
		;
33
	unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
34
		"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
35
36
	binary-operator ::= "="|"!="|"<"|">"|"-eq"|"-ne"|"-ge"|"-gt"|
37
			"-le"|"-lt"|"-nt"|"-ot"|"-ef";
38
	operand ::= <any legal UNIX file name>
39
*/
40
41
enum token {
42
	EOI,
43
	FILRD,
44
	FILWR,
45
	FILEX,
46
	FILEXIST,
47
	FILREG,
48
	FILDIR,
49
	FILCDEV,
50
	FILBDEV,
51
	FILFIFO,
52
	FILSOCK,
53
	FILSYM,
54
	FILGZ,
55
	FILTT,
56
	FILSUID,
57
	FILSGID,
58
	FILSTCK,
59
	FILNT,
60
	FILOT,
61
	FILEQ,
62
	FILUID,
63
	FILGID,
64
	STREZ,
65
	STRNZ,
66
	STREQ,
67
	STRNE,
68
	STRLT,
69
	STRGT,
70
	INTEQ,
71
	INTNE,
72
	INTGE,
73
	INTGT,
74
	INTLE,
75
	INTLT,
76
	UNOT,
77
	BAND,
78
	BOR,
79
	LPAREN,
80
	RPAREN,
81
	OPERAND
82
};
83
84
enum token_types {
85
	UNOP,
86
	BINOP,
87
	BUNOP,
88
	BBINOP,
89
	PAREN
90
};
91
92
struct t_op {
93
	const char *op_text;
94
	short op_num, op_type;
95
} const ops [] = {
96
	{"-r",	FILRD,	UNOP},
97
	{"-w",	FILWR,	UNOP},
98
	{"-x",	FILEX,	UNOP},
99
	{"-e",	FILEXIST,UNOP},
100
	{"-f",	FILREG,	UNOP},
101
	{"-d",	FILDIR,	UNOP},
102
	{"-c",	FILCDEV,UNOP},
103
	{"-b",	FILBDEV,UNOP},
104
	{"-p",	FILFIFO,UNOP},
105
	{"-u",	FILSUID,UNOP},
106
	{"-g",	FILSGID,UNOP},
107
	{"-k",	FILSTCK,UNOP},
108
	{"-s",	FILGZ,	UNOP},
109
	{"-t",	FILTT,	UNOP},
110
	{"-z",	STREZ,	UNOP},
111
	{"-n",	STRNZ,	UNOP},
112
	{"-h",	FILSYM,	UNOP},		/* for backwards compat */
113
	{"-O",	FILUID,	UNOP},
114
	{"-G",	FILGID,	UNOP},
115
	{"-L",	FILSYM,	UNOP},
116
	{"-S",	FILSOCK,UNOP},
117
	{"=",	STREQ,	BINOP},
118
	{"!=",	STRNE,	BINOP},
119
	{"<",	STRLT,	BINOP},
120
	{">",	STRGT,	BINOP},
121
	{"-eq",	INTEQ,	BINOP},
122
	{"-ne",	INTNE,	BINOP},
123
	{"-ge",	INTGE,	BINOP},
124
	{"-gt",	INTGT,	BINOP},
125
	{"-le",	INTLE,	BINOP},
126
	{"-lt",	INTLT,	BINOP},
127
	{"-nt",	FILNT,	BINOP},
128
	{"-ot",	FILOT,	BINOP},
129
	{"-ef",	FILEQ,	BINOP},
130
	{"!",	UNOT,	BUNOP},
131
	{"-a",	BAND,	BBINOP},
132
	{"-o",	BOR,	BBINOP},
133
	{"(",	LPAREN,	PAREN},
134
	{")",	RPAREN,	PAREN},
135
	{0,	0,	0}
136
};
137
138
char **t_wp;
139
struct t_op const *t_wp_op;
140
141
static enum token t_lex(char *);
142
static enum token t_lex_type(char *);
143
static int oexpr(enum token n);
144
static int aexpr(enum token n);
145
static int nexpr(enum token n);
146
static int binop(void);
147
static int primary(enum token n);
148
static int filstat(char *nm, enum token mode);
149
static int getn(const char *s);
150
static int newerf(const char *, const char *);
151
static int olderf(const char *, const char *);
152
static int equalf(const char *, const char *);
153
static __dead void syntax(const char *op, char *msg);
154
155
int
156
main(int argc, char *argv[])
157
{
158
	extern char *__progname;
159
	int	res;
160
161
5942
	if (pledge("stdio rpath flock cpath wpath", NULL) == -1)
162
		err(2, "pledge");
163
164
2971
	if (strcmp(__progname, "[") == 0) {
165
		if (strcmp(argv[--argc], "]"))
166
			errx(2, "missing ]");
167
		argv[argc] = NULL;
168
	}
169
170
	/* Implement special cases from POSIX.2, section 4.62.4 */
171

4146
	switch (argc) {
172
	case 1:
173
		return 1;
174
	case 2:
175
184
		return (*argv[1] == '\0');
176
	case 3:
177

794
		if (argv[1][0] == '!' && argv[1][1] == '\0') {
178
46
			return !(*argv[2] == '\0');
179
		}
180
		break;
181
	case 4:
182

1360
		if (argv[1][0] != '!' || argv[1][1] != '\0') {
183
2392
			if (t_lex(argv[2]),
184
2392
			    t_wp_op && t_wp_op->op_type == BINOP) {
185
874
				t_wp = &argv[1];
186
874
				return (binop() == 0);
187
			}
188
		}
189
		break;
190
	case 5:
191

46
		if (argv[1][0] == '!' && argv[1][1] == '\0') {
192
			if (t_lex(argv[3]),
193
			    t_wp_op && t_wp_op->op_type == BINOP) {
194
				t_wp = &argv[2];
195
				return !(binop() == 0);
196
			}
197
		}
198
		break;
199
	}
200
201
1867
	t_wp = &argv[1];
202
1867
	res = !oexpr(t_lex(*t_wp));
203
204

3734
	if (*t_wp != NULL && *++t_wp != NULL)
205
		syntax(*t_wp, "unknown operand");
206
207
1867
	return res;
208
2971
}
209
210
static __dead void
211
syntax(const char *op, char *msg)
212
{
213
	if (op && *op)
214
		errx(2, "%s: %s", op, msg);
215
	else
216
		errx(2, "%s", msg);
217
}
218
219
static int
220
oexpr(enum token n)
221
{
222
	int res;
223
224
6218
	res = aexpr(n);
225
3109
	if (t_lex(*++t_wp) == BOR)
226
552
		return oexpr(t_lex(*++t_wp)) || res;
227
2557
	t_wp--;
228
2557
	return res;
229
3109
}
230
231
static int
232
aexpr(enum token n)
233
{
234
	int res;
235
236
7142
	res = nexpr(n);
237
3571
	if (t_lex(*++t_wp) == BAND)
238
462
		return aexpr(t_lex(*++t_wp)) && res;
239
3109
	t_wp--;
240
3109
	return res;
241
3571
}
242
243
static int
244
nexpr(enum token n)
245
{
246
7538
	if (n == UNOT)
247
198
		return !nexpr(t_lex(*++t_wp));
248
3571
	return primary(n);
249
3769
}
250
251
static int
252
primary(enum token n)
253
{
254
	int res;
255
256
7142
	if (n == EOI)
257
		syntax(NULL, "argument expected");
258
3571
	if (n == LPAREN) {
259
690
		res = oexpr(t_lex(*++t_wp));
260
690
		if (t_lex(*++t_wp) != RPAREN)
261
			syntax(NULL, "closing paren expected");
262
690
		return res;
263
	}
264
	/*
265
	 * We need this, if not binary operations with more than 4
266
	 * arguments will always fall into unary.
267
	 */
268
2881
	if(t_lex_type(t_wp[1]) == BINOP) {
269
920
		t_lex(t_wp[1]);
270

1840
		if (t_wp_op && t_wp_op->op_type == BINOP)
271
920
			return binop();
272
	}
273
274

2910
	if (t_wp_op && t_wp_op->op_type == UNOP) {
275
		/* unary expression */
276
949
		if (*++t_wp == NULL)
277
			syntax(t_wp_op->op_text, "argument expected");
278

949
		switch (n) {
279
		case STREZ:
280
46
			return strlen(*t_wp) == 0;
281
		case STRNZ:
282
92
			return strlen(*t_wp) != 0;
283
		case FILTT:
284
			return isatty(getn(*t_wp));
285
		default:
286
811
			return filstat(*t_wp, n);
287
		}
288
	}
289
290
1012
	return strlen(*t_wp) > 0;
291
3571
}
292
293
static int
294
binop(void)
295
{
296
	const char *opnd1, *opnd2;
297
	struct t_op const *op;
298
299
3588
	opnd1 = *t_wp;
300
1794
	(void) t_lex(*++t_wp);
301
1794
	op = t_wp_op;
302
303
1794
	if ((opnd2 = *++t_wp) == NULL)
304
		syntax(op->op_text, "argument expected");
305
306



1794
	switch (op->op_num) {
307
	case STREQ:
308
1104
		return strcmp(opnd1, opnd2) == 0;
309
	case STRNE:
310
92
		return strcmp(opnd1, opnd2) != 0;
311
	case STRLT:
312
		return strcmp(opnd1, opnd2) < 0;
313
	case STRGT:
314
		return strcmp(opnd1, opnd2) > 0;
315
	case INTEQ:
316
276
		return getn(opnd1) == getn(opnd2);
317
	case INTNE:
318
46
		return getn(opnd1) != getn(opnd2);
319
	case INTGE:
320
92
		return getn(opnd1) >= getn(opnd2);
321
	case INTGT:
322
46
		return getn(opnd1) > getn(opnd2);
323
	case INTLE:
324
46
		return getn(opnd1) <= getn(opnd2);
325
	case INTLT:
326
92
		return getn(opnd1) < getn(opnd2);
327
	case FILNT:
328
		return newerf(opnd1, opnd2);
329
	case FILOT:
330
		return olderf(opnd1, opnd2);
331
	case FILEQ:
332
		return equalf(opnd1, opnd2);
333
	}
334
335
	syntax(op->op_text, "not a binary operator");
336
1794
}
337
338
static enum token
339
t_lex_type(char *s)
340
{
341
	struct t_op const *op = ops;
342
343
5762
	if (s == NULL)
344
506
		return -1;
345
346
149261
	while (op->op_text) {
347
74961
		if (strcmp(s, op->op_text) == 0)
348
1518
			return op->op_type;
349
73443
		op++;
350
	}
351
857
	return -1;
352
2881
}
353
354
static int
355
filstat(char *nm, enum token mode)
356
{
357
1622
	struct stat s;
358
	mode_t i;
359
360
811
	if (mode == FILSYM) {
361
#ifdef S_IFLNK
362
48
		if (lstat(nm, &s) == 0) {
363
			i = S_IFLNK;
364
48
			goto filetype;
365
		}
366
#endif
367
		return 0;
368
	}
369
370
763
	if (stat(nm, &s) != 0)
371
13
		return 0;
372
373




750
	switch (mode) {
374
	case FILRD:
375
46
		return access(nm, R_OK) == 0;
376
	case FILWR:
377
46
		return access(nm, W_OK) == 0;
378
	case FILEX:
379
46
		return access(nm, X_OK) == 0;
380
	case FILEXIST:
381
5
		return access(nm, F_OK) == 0;
382
	case FILREG:
383
		i = S_IFREG;
384
116
		goto filetype;
385
	case FILDIR:
386
		i = S_IFDIR;
387
96
		goto filetype;
388
	case FILCDEV:
389
		i = S_IFCHR;
390
46
		goto filetype;
391
	case FILBDEV:
392
		i = S_IFBLK;
393
92
		goto filetype;
394
	case FILFIFO:
395
#ifdef S_IFIFO
396
		i = S_IFIFO;
397
		goto filetype;
398
#else
399
		return 0;
400
#endif
401
	case FILSOCK:
402
#ifdef S_IFSOCK
403
		i = S_IFSOCK;
404
		goto filetype;
405
#else
406
		return 0;
407
#endif
408
	case FILSUID:
409
		i = S_ISUID;
410
		goto filebit;
411
	case FILSGID:
412
		i = S_ISGID;
413
		goto filebit;
414
	case FILSTCK:
415
		i = S_ISVTX;
416
		goto filebit;
417
	case FILGZ:
418
257
		return s.st_size > 0L;
419
	case FILUID:
420
		return s.st_uid == geteuid();
421
	case FILGID:
422
		return s.st_gid == getegid();
423
	default:
424
		return 1;
425
	}
426
427
filetype:
428
398
	return ((s.st_mode & S_IFMT) == i);
429
430
filebit:
431
	return ((s.st_mode & i) != 0);
432
811
}
433
434
static enum token
435
t_lex(char *s)
436
{
437
	struct t_op const *op = ops;
438
439
30190
	if (s == 0) {
440
3734
		t_wp_op = NULL;
441
3734
		return EOI;
442
	}
443
685027
	while (op->op_text) {
444
346538
		if (strcmp(s, op->op_text) == 0) {
445
9705
			t_wp_op = op;
446
9705
			return op->op_num;
447
		}
448
336833
		op++;
449
	}
450
1656
	t_wp_op = NULL;
451
1656
	return OPERAND;
452
15095
}
453
454
/* atoi with error detection */
455
static int
456
getn(const char *s)
457
{
458
2392
	char *p;
459
	long r;
460
461
1196
	errno = 0;
462
1196
	r = strtol(s, &p, 10);
463
464
1196
	if (errno != 0)
465
		errx(2, "%s: out of range", s);
466
467
1196
	while (isspace((unsigned char)*p))
468
		p++;
469
470
1196
	if (*p)
471
		errx(2, "%s: bad number", s);
472
473
2392
	return (int) r;
474
1196
}
475
476
static int
477
newerf(const char *f1, const char *f2)
478
{
479
	struct stat b1, b2;
480
481
	return (stat(f1, &b1) == 0 &&
482
	    stat(f2, &b2) == 0 &&
483
	    b1.st_mtime > b2.st_mtime);
484
}
485
486
static int
487
olderf(const char *f1, const char *f2)
488
{
489
	struct stat b1, b2;
490
491
	return (stat(f1, &b1) == 0 &&
492
	    stat(f2, &b2) == 0 &&
493
	    b1.st_mtime < b2.st_mtime);
494
}
495
496
static int
497
equalf(const char *f1, const char *f2)
498
{
499
	struct stat b1, b2;
500
501
	return (stat(f1, &b1) == 0 &&
502
	    stat(f2, &b2) == 0 &&
503
	    b1.st_dev == b2.st_dev &&
504
	    b1.st_ino == b2.st_ino);
505
}