GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/test/test.c Lines: 107 159 67.3 %
Date: 2017-11-13 Branches: 77 139 55.4 %

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
1656
	if (pledge("stdio rpath flock cpath wpath", NULL) == -1)
162
		err(2, "pledge");
163
164
828
	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

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

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

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

12
		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
540
	t_wp = &argv[1];
202
540
	res = !oexpr(t_lex(*t_wp));
203
204

1080
	if (*t_wp != NULL && *++t_wp != NULL)
205
		syntax(*t_wp, "unknown operand");
206
207
540
	return res;
208
828
}
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
1728
	res = aexpr(n);
225
864
	if (t_lex(*++t_wp) == BOR)
226
144
		return oexpr(t_lex(*++t_wp)) || res;
227
720
	t_wp--;
228
720
	return res;
229
864
}
230
231
static int
232
aexpr(enum token n)
233
{
234
	int res;
235
236
1968
	res = nexpr(n);
237
984
	if (t_lex(*++t_wp) == BAND)
238
120
		return aexpr(t_lex(*++t_wp)) && res;
239
864
	t_wp--;
240
864
	return res;
241
984
}
242
243
static int
244
nexpr(enum token n)
245
{
246
2064
	if (n == UNOT)
247
48
		return !nexpr(t_lex(*++t_wp));
248
984
	return primary(n);
249
1032
}
250
251
static int
252
primary(enum token n)
253
{
254
	int res;
255
256
1968
	if (n == EOI)
257
		syntax(NULL, "argument expected");
258
984
	if (n == LPAREN) {
259
180
		res = oexpr(t_lex(*++t_wp));
260
180
		if (t_lex(*++t_wp) != RPAREN)
261
			syntax(NULL, "closing paren expected");
262
180
		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
804
	if(t_lex_type(t_wp[1]) == BINOP) {
269
240
		t_lex(t_wp[1]);
270

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

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

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



468
	switch (op->op_num) {
307
	case STREQ:
308
288
		return strcmp(opnd1, opnd2) == 0;
309
	case STRNE:
310
24
		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
72
		return getn(opnd1) == getn(opnd2);
317
	case INTNE:
318
12
		return getn(opnd1) != getn(opnd2);
319
	case INTGE:
320
24
		return getn(opnd1) >= getn(opnd2);
321
	case INTGT:
322
12
		return getn(opnd1) > getn(opnd2);
323
	case INTLE:
324
12
		return getn(opnd1) <= getn(opnd2);
325
	case INTLT:
326
24
		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
468
}
337
338
static enum token
339
t_lex_type(char *s)
340
{
341
	struct t_op const *op = ops;
342
343
1608
	if (s == NULL)
344
132
		return -1;
345
346
43752
	while (op->op_text) {
347
21600
		if (strcmp(s, op->op_text) == 0)
348
396
			return op->op_type;
349
21204
		op++;
350
	}
351
276
	return -1;
352
804
}
353
354
static int
355
filstat(char *nm, enum token mode)
356
{
357
528
	struct stat s;
358
	mode_t i;
359
360
264
	if (mode == FILSYM) {
361
#ifdef S_IFLNK
362
12
		if (lstat(nm, &s) == 0) {
363
			i = S_IFLNK;
364
12
			goto filetype;
365
		}
366
#endif
367
		return 0;
368
	}
369
370
252
	if (stat(nm, &s) != 0)
371
		return 0;
372
373




252
	switch (mode) {
374
	case FILRD:
375
12
		return access(nm, R_OK) == 0;
376
	case FILWR:
377
12
		return access(nm, W_OK) == 0;
378
	case FILEX:
379
12
		return access(nm, X_OK) == 0;
380
	case FILEXIST:
381
		return access(nm, F_OK) == 0;
382
	case FILREG:
383
		i = S_IFREG;
384
24
		goto filetype;
385
	case FILDIR:
386
		i = S_IFDIR;
387
24
		goto filetype;
388
	case FILCDEV:
389
		i = S_IFCHR;
390
12
		goto filetype;
391
	case FILBDEV:
392
		i = S_IFBLK;
393
24
		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
132
		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
96
	return ((s.st_mode & S_IFMT) == i);
429
430
filebit:
431
	return ((s.st_mode & i) != 0);
432
264
}
433
434
static enum token
435
t_lex(char *s)
436
{
437
	struct t_op const *op = ops;
438
439
8184
	if (s == 0) {
440
1080
		t_wp_op = NULL;
441
1080
		return EOI;
442
	}
443
182928
	while (op->op_text) {
444
91032
		if (strcmp(s, op->op_text) == 0) {
445
2580
			t_wp_op = op;
446
2580
			return op->op_num;
447
		}
448
88452
		op++;
449
	}
450
432
	t_wp_op = NULL;
451
432
	return OPERAND;
452
4092
}
453
454
/* atoi with error detection */
455
static int
456
getn(const char *s)
457
{
458
624
	char *p;
459
	long r;
460
461
312
	errno = 0;
462
312
	r = strtol(s, &p, 10);
463
464
312
	if (errno != 0)
465
		errx(2, "%s: out of range", s);
466
467
624
	while (isspace((unsigned char)*p))
468
		p++;
469
470
312
	if (*p)
471
		errx(2, "%s: bad number", s);
472
473
624
	return (int) r;
474
312
}
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
}