summaryrefslogtreecommitdiff
path: root/usr/src/ucbcmd/test/test.c
blob: b8be0f59ac38425d0252528fed155db9c0e48b95 (plain)
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
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved. The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * test expression
 * [ expression ]
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#define	EQ(a, b)	((strcmp(a, b) == 0))

static char *nxtarg(int mt);
static int exp(void);
static int e1(void);
static int e2(void);
static int e3(void);
static int tio(char *a, int f);
static int ftype(char *f, int field);
static int filtyp(char *f, int field);
static int fsizep(char *f);
static void synbad(char *s1, char *s2);

static int	ap;
static int	ac;
static char	**av;

int
main(int argc, char *argv[])
{
	int status;

	ac = argc; av = argv; ap = 1;
	if (EQ(argv[0], "[")) {
		if (!EQ(argv[--ac], "]"))
			synbad("] missing", "");
	}
	argv[ac] = 0;
	if (ac <= 1)
		exit(1);
	status = (exp() ? 0 : 1);
	if (nxtarg(1) != 0)
		synbad("too many arguments", "");
	return (status);
}

static char *
nxtarg(int mt)
{
	if (ap >= ac) {
		if (mt) {
			ap++;
			return (0);
		}
		synbad("argument expected", "");
	}
	return (av[ap++]);
}

static int
exp(void)
{
	int p1;
	char *p2;

	p1 = e1();
	p2 = nxtarg(1);
	if (p2 != 0) {
		if (EQ(p2, "-o"))
			return (p1 | exp());
		if (EQ(p2, "]"))
			synbad("syntax error", "");
	}
	ap--;
	return (p1);
}

static int
e1(void)
{
	int p1;
	char *p2;

	p1 = e2();
	p2 = nxtarg(1);
	if ((p2 != 0) && EQ(p2, "-a"))
		return (p1 & e1());
	ap--;
	return (p1);
}

static int
e2(void)
{
	if (EQ(nxtarg(0), "!"))
		return (!e3());
	ap--;
	return (e3());
}

static int
e3(void)
{
	int p1;
	char *a;
	char *p2;
	int int1, int2;

	a = nxtarg(0);
	if (EQ(a, "(")) {
		p1 = exp();
		if (!EQ(nxtarg(0), ")")) synbad(") expected", "");
		return (p1);
	}
	p2 = nxtarg(1);
	ap--;
	if ((p2 == 0) || (!EQ(p2, "=") && !EQ(p2, "!="))) {
		if (EQ(a, "-r"))
			return (tio(nxtarg(0), 4));

		if (EQ(a, "-w"))
			return (tio(nxtarg(0), 2));

		if (EQ(a, "-x"))
			return (tio(nxtarg(0), 1));

		if (EQ(a, "-d"))
			return (filtyp(nxtarg(0), S_IFDIR));

		if (EQ(a, "-c"))
			return (filtyp(nxtarg(0), S_IFCHR));

		if (EQ(a, "-b"))
			return (filtyp(nxtarg(0), S_IFBLK));

		if (EQ(a, "-f")) {
			struct stat statb;

			return (stat(nxtarg(0), &statb) >= 0 &&
			    (statb.st_mode & S_IFMT) != S_IFDIR);
		}

		if (EQ(a, "-h"))
			return (filtyp(nxtarg(0), S_IFLNK));

		if (EQ(a, "-u"))
			return (ftype(nxtarg(0), S_ISUID));

		if (EQ(a, "-g"))
			return (ftype(nxtarg(0), S_ISGID));

		if (EQ(a, "-k"))
			return (ftype(nxtarg(0), S_ISVTX));

		if (EQ(a, "-p"))
#ifdef S_IFIFO
			return (filtyp(nxtarg(0), S_IFIFO));
#else
			return (nxtarg(0), 0);
#endif

		if (EQ(a, "-s"))
			return (fsizep(nxtarg(0)));

		if (EQ(a, "-t"))
			if (ap >= ac)
				return (isatty(1));
			else if (EQ((a = nxtarg(0)), "-a") || EQ(a, "-o")) {
				ap--;
				return (isatty(1));
			} else
				return (isatty(atoi(a)));

		if (EQ(a, "-n"))
			return (!EQ(nxtarg(0), ""));
		if (EQ(a, "-z"))
			return (EQ(nxtarg(0), ""));
	}

	p2 = nxtarg(1);
	if (p2 == 0)
		return (!EQ(a, ""));
	if (EQ(p2, "-a") || EQ(p2, "-o")) {
		ap--;
		return (!EQ(a, ""));
	}
	if (EQ(p2, "="))
		return (EQ(nxtarg(0), a));

	if (EQ(p2, "!="))
		return (!EQ(nxtarg(0), a));

	int1 = atoi(a);
	int2 = atoi(nxtarg(0));
	if (EQ(p2, "-eq"))
		return (int1 == int2);
	if (EQ(p2, "-ne"))
		return (int1 != int2);
	if (EQ(p2, "-gt"))
		return (int1 > int2);
	if (EQ(p2, "-lt"))
		return (int1 < int2);
	if (EQ(p2, "-ge"))
		return (int1 >= int2);
	if (EQ(p2, "-le"))
		return (int1 <= int2);

	synbad("unknown operator ", p2);
	/* NOTREACHED */
	return (0);
}

static int
tio(char *a, int f)
{
	if (access(a, f) == 0)
		return (1);
	else
		return (0);
}

static int
ftype(char *f, int field)
{
	struct stat statb;

	if (stat(f, &statb) < 0)
		return (0);
	if ((statb.st_mode & field) == field)
		return (1);
	return (0);
}

static int
filtyp(char *f, int field)
{
	struct stat statb;

	if (field == S_IFLNK) {
		if (lstat(f, &statb) < 0)
			return (0);
	} else {
		if (stat(f, &statb) < 0)
			return (0);
	}
	if ((statb.st_mode & S_IFMT) == field)
		return (1);
	else
		return (0);
}

static int
fsizep(char *f)
{
	struct stat statb;

	if (stat(f, &statb) < 0)
		return (0);
	return (statb.st_size > 0);
}

static void
synbad(char *s1, char *s2)
{
	(void) write(2, "test: ", 6);
	(void) write(2, s1, strlen(s1));
	(void) write(2, s2, strlen(s2));
	(void) write(2, "\n", 1);
	exit(255);
}