]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* GNU test program (ksb and mjb) */ |
2 | ||
3 | /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */ | |
4 | ||
5 | /* Copyright (C) 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc. | |
6 | ||
7 | This file is part of GNU Bash, the Bourne Again SHell. | |
8 | ||
9 | Bash is free software; you can redistribute it and/or modify it under | |
10 | the terms of the GNU General Public License as published by the Free | |
11 | Software Foundation; either version 2, or (at your option) any later | |
12 | version. | |
13 | ||
14 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY | |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License along | |
20 | with Bash; see the file COPYING. If not, write to the Free Software | |
21 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
22 | ||
ccc6cda3 JA |
23 | /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching |
24 | binary operators. */ | |
25 | /* #define PATTERN_MATCHING */ | |
26 | ||
27 | #if defined (HAVE_CONFIG_H) | |
28 | # include <config.h> | |
29 | #endif | |
30 | ||
726f6388 | 31 | #include <stdio.h> |
ccc6cda3 | 32 | |
d166f048 | 33 | #include "bashtypes.h" |
ccc6cda3 JA |
34 | |
35 | #if defined (HAVE_LIMITS_H) | |
36 | # include <limits.h> | |
37 | #else | |
38 | # include <sys/param.h> | |
39 | #endif | |
40 | ||
41 | #if defined (HAVE_UNISTD_H) | |
42 | # include <unistd.h> | |
43 | #endif | |
44 | ||
e8ce775d JA |
45 | #include <errno.h> |
46 | #if !defined (errno) | |
47 | extern int errno; | |
48 | #endif /* !errno */ | |
49 | ||
ccc6cda3 JA |
50 | #if !defined (_POSIX_VERSION) |
51 | # include <sys/file.h> | |
52 | #endif /* !_POSIX_VERSION */ | |
53 | #include "posixstat.h" | |
54 | #include "filecntl.h" | |
726f6388 | 55 | |
d166f048 | 56 | #include "shell.h" |
cce855bc JA |
57 | #include "pathexp.h" |
58 | #include "test.h" | |
d166f048 | 59 | #include "builtins/common.h" |
726f6388 | 60 | |
cce855bc JA |
61 | #include <glob/fnmatch.h> |
62 | ||
726f6388 JA |
63 | #if !defined (STRLEN) |
64 | # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0) | |
65 | #endif | |
66 | ||
726f6388 JA |
67 | #if !defined (STREQ) |
68 | # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0) | |
69 | #endif /* !STREQ */ | |
70 | ||
71 | #if !defined (member) | |
72 | # define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0) | |
73 | #endif /* !member */ | |
74 | ||
726f6388 JA |
75 | #if !defined (R_OK) |
76 | #define R_OK 4 | |
77 | #define W_OK 2 | |
78 | #define X_OK 1 | |
79 | #define F_OK 0 | |
80 | #endif /* R_OK */ | |
81 | ||
ccc6cda3 JA |
82 | #define EQ 0 |
83 | #define NE 1 | |
84 | #define LT 2 | |
85 | #define GT 3 | |
86 | #define LE 4 | |
87 | #define GE 5 | |
88 | ||
89 | #define NT 0 | |
90 | #define OT 1 | |
91 | #define EF 2 | |
92 | ||
726f6388 JA |
93 | /* The following few defines control the truth and false output of each stage. |
94 | TRUE and FALSE are what we use to compute the final output value. | |
95 | SHELL_BOOLEAN is the form which returns truth or falseness in shell terms. | |
ccc6cda3 | 96 | Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */ |
726f6388 JA |
97 | #define TRUE 1 |
98 | #define FALSE 0 | |
99 | #define SHELL_BOOLEAN(value) (!(value)) | |
726f6388 | 100 | |
b72432fd JA |
101 | #define TEST_ERREXIT_STATUS 2 |
102 | ||
ccc6cda3 JA |
103 | static procenv_t test_exit_buf; |
104 | static int test_error_return; | |
d166f048 | 105 | #define test_exit(val) \ |
726f6388 | 106 | do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0) |
726f6388 | 107 | |
cce855bc JA |
108 | /* We have to use access(2) for machines running AFS, because it's |
109 | not a Unix file system. This may produce incorrect answers for | |
110 | non-AFS files. I hate AFS. */ | |
726f6388 | 111 | #if defined (AFS) |
726f6388 JA |
112 | # define EACCESS(path, mode) access(path, mode) |
113 | #else | |
ccc6cda3 | 114 | # define EACCESS(path, mode) test_eaccess(path, mode) |
726f6388 JA |
115 | #endif /* AFS */ |
116 | ||
117 | static int pos; /* The offset of the current argument in ARGV. */ | |
118 | static int argc; /* The number of arguments present in ARGV. */ | |
119 | static char **argv; /* The argument list. */ | |
120 | static int noeval; | |
121 | ||
726f6388 JA |
122 | static int unary_operator (); |
123 | static int binary_operator (); | |
124 | static int two_arguments (); | |
125 | static int three_arguments (); | |
126 | static int posixtest (); | |
127 | ||
128 | static int expr (); | |
129 | static int term (); | |
130 | static int and (); | |
131 | static int or (); | |
132 | ||
ccc6cda3 JA |
133 | static void beyond (); |
134 | ||
726f6388 JA |
135 | static void |
136 | test_syntax_error (format, arg) | |
137 | char *format, *arg; | |
138 | { | |
726f6388 JA |
139 | extern int interactive_shell; |
140 | extern char *get_name_for_error (); | |
ccc6cda3 | 141 | if (interactive_shell == 0) |
726f6388 | 142 | fprintf (stderr, "%s: ", get_name_for_error ()); |
726f6388 JA |
143 | fprintf (stderr, "%s: ", argv[0]); |
144 | fprintf (stderr, format, arg); | |
ccc6cda3 | 145 | fprintf (stderr, "\n"); |
726f6388 | 146 | fflush (stderr); |
b72432fd | 147 | test_exit (TEST_ERREXIT_STATUS); |
726f6388 JA |
148 | } |
149 | ||
cce855bc JA |
150 | /* |
151 | * beyond - call when we're beyond the end of the argument list (an | |
152 | * error condition) | |
153 | */ | |
154 | static void | |
155 | beyond () | |
156 | { | |
157 | test_syntax_error ("argument expected", (char *)NULL); | |
158 | } | |
159 | ||
160 | /* Syntax error for when an integer argument was expected, but | |
161 | something else was found. */ | |
162 | static void | |
163 | integer_expected_error (pch) | |
164 | char *pch; | |
165 | { | |
166 | test_syntax_error ("%s: integer expression expected", pch); | |
167 | } | |
168 | ||
726f6388 JA |
169 | /* A wrapper for stat () which disallows pathnames that are empty strings |
170 | and handles /dev/fd emulation on systems that don't have it. */ | |
171 | static int | |
172 | test_stat (path, finfo) | |
173 | char *path; | |
174 | struct stat *finfo; | |
175 | { | |
176 | if (*path == '\0') | |
177 | { | |
178 | errno = ENOENT; | |
179 | return (-1); | |
180 | } | |
726f6388 JA |
181 | if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0) |
182 | { | |
ccc6cda3 JA |
183 | #if !defined (HAVE_DEV_FD) |
184 | long fd; | |
d166f048 | 185 | if (legal_number (path + 8, &fd)) |
ccc6cda3 | 186 | return (fstat ((int)fd, finfo)); |
726f6388 JA |
187 | else |
188 | { | |
189 | errno = EBADF; | |
190 | return (-1); | |
191 | } | |
ccc6cda3 JA |
192 | #else |
193 | /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a | |
194 | trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx. | |
195 | On most systems, with the notable exception of linux, this is | |
196 | effectively a no-op. */ | |
197 | char pbuf[32]; | |
198 | strcpy (pbuf, DEV_FD_PREFIX); | |
199 | strcat (pbuf, path + 8); | |
200 | return (stat (pbuf, finfo)); | |
726f6388 | 201 | #endif /* !HAVE_DEV_FD */ |
ccc6cda3 | 202 | } |
726f6388 JA |
203 | return (stat (path, finfo)); |
204 | } | |
205 | ||
206 | /* Do the same thing access(2) does, but use the effective uid and gid, | |
207 | and don't make the mistake of telling root that any file is | |
208 | executable. */ | |
cce855bc | 209 | int |
ccc6cda3 | 210 | test_eaccess (path, mode) |
726f6388 JA |
211 | char *path; |
212 | int mode; | |
213 | { | |
214 | struct stat st; | |
726f6388 JA |
215 | |
216 | if (test_stat (path, &st) < 0) | |
217 | return (-1); | |
218 | ||
d166f048 | 219 | if (current_user.euid == 0) |
726f6388 JA |
220 | { |
221 | /* Root can read or write any file. */ | |
222 | if (mode != X_OK) | |
223 | return (0); | |
224 | ||
225 | /* Root can execute any file that has any one of the execute | |
226 | bits set. */ | |
227 | if (st.st_mode & S_IXUGO) | |
228 | return (0); | |
229 | } | |
230 | ||
d166f048 | 231 | if (st.st_uid == current_user.euid) /* owner */ |
726f6388 JA |
232 | mode <<= 6; |
233 | else if (group_member (st.st_gid)) | |
234 | mode <<= 3; | |
235 | ||
236 | if (st.st_mode & mode) | |
237 | return (0); | |
238 | ||
cce855bc | 239 | errno = EACCES; |
726f6388 JA |
240 | return (-1); |
241 | } | |
242 | ||
726f6388 JA |
243 | /* Increment our position in the argument list. Check that we're not |
244 | past the end of the argument list. This check is supressed if the | |
245 | argument is FALSE. Made a macro for efficiency. */ | |
726f6388 | 246 | #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0) |
726f6388 JA |
247 | #define unary_advance() do { advance (1); ++pos; } while (0) |
248 | ||
249 | /* | |
cce855bc JA |
250 | * expr: |
251 | * or | |
726f6388 | 252 | */ |
cce855bc JA |
253 | static int |
254 | expr () | |
726f6388 | 255 | { |
cce855bc JA |
256 | if (pos >= argc) |
257 | beyond (); | |
258 | ||
259 | return (FALSE ^ or ()); /* Same with this. */ | |
726f6388 JA |
260 | } |
261 | ||
cce855bc JA |
262 | /* |
263 | * or: | |
264 | * and | |
265 | * and '-o' or | |
266 | */ | |
267 | static int | |
268 | or () | |
726f6388 | 269 | { |
cce855bc JA |
270 | int value, v2; |
271 | ||
272 | value = and (); | |
273 | while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2]) | |
274 | { | |
275 | advance (0); | |
276 | v2 = or (); | |
277 | return (value || v2); | |
278 | } | |
279 | ||
280 | return (value); | |
281 | } | |
282 | ||
283 | /* | |
284 | * and: | |
285 | * term | |
286 | * term '-a' and | |
287 | */ | |
288 | static int | |
289 | and () | |
290 | { | |
291 | int value, v2; | |
292 | ||
293 | value = term (); | |
294 | while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2]) | |
295 | { | |
296 | advance (0); | |
297 | v2 = and (); | |
298 | return (value && v2); | |
299 | } | |
300 | return (value); | |
726f6388 JA |
301 | } |
302 | ||
726f6388 JA |
303 | /* |
304 | * term - parse a term and return 1 or 0 depending on whether the term | |
305 | * evaluates to true or false, respectively. | |
306 | * | |
307 | * term ::= | |
cce855bc JA |
308 | * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename |
309 | * '-'('G'|'L'|'O'|'S'|'N') filename | |
ccc6cda3 | 310 | * '-t' [int] |
726f6388 | 311 | * '-'('z'|'n') string |
cce855bc | 312 | * '-o' option |
726f6388 | 313 | * string |
ccc6cda3 | 314 | * string ('!='|'='|'==') string |
726f6388 JA |
315 | * <int> '-'(eq|ne|le|lt|ge|gt) <int> |
316 | * file '-'(nt|ot|ef) file | |
317 | * '(' <expr> ')' | |
318 | * int ::= | |
726f6388 JA |
319 | * positive and negative integers |
320 | */ | |
321 | static int | |
322 | term () | |
323 | { | |
324 | int value; | |
325 | ||
326 | if (pos >= argc) | |
327 | beyond (); | |
328 | ||
ccc6cda3 JA |
329 | /* Deal with leading `not's. */ |
330 | if (argv[pos][0] == '!' && argv[pos][1] == '\0') | |
726f6388 | 331 | { |
ccc6cda3 JA |
332 | value = 0; |
333 | while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0') | |
726f6388 JA |
334 | { |
335 | advance (1); | |
ccc6cda3 | 336 | value = 1 - value; |
726f6388 JA |
337 | } |
338 | ||
ccc6cda3 | 339 | return (value ? !term() : term()); |
726f6388 JA |
340 | } |
341 | ||
ccc6cda3 JA |
342 | /* A paren-bracketed argument. */ |
343 | if (argv[pos][0] == '(' && argv[pos][1] == '\0') | |
726f6388 JA |
344 | { |
345 | advance (1); | |
346 | value = expr (); | |
347 | if (argv[pos] == 0) | |
ccc6cda3 | 348 | test_syntax_error ("`)' expected", (char *)NULL); |
726f6388 | 349 | else if (argv[pos][0] != ')' || argv[pos][1]) |
ccc6cda3 | 350 | test_syntax_error ("`)' expected, found %s", argv[pos]); |
726f6388 | 351 | advance (0); |
ccc6cda3 | 352 | return (value); |
726f6388 JA |
353 | } |
354 | ||
355 | /* are there enough arguments left that this could be dyadic? */ | |
cce855bc | 356 | if ((pos + 3 <= argc) && test_binop (argv[pos + 1])) |
726f6388 JA |
357 | value = binary_operator (); |
358 | ||
359 | /* Might be a switch type argument */ | |
ccc6cda3 | 360 | else if (argv[pos][0] == '-' && argv[pos][2] == '\0') |
726f6388 | 361 | { |
cce855bc | 362 | if (test_unop (argv[pos])) |
726f6388 JA |
363 | value = unary_operator (); |
364 | else | |
ccc6cda3 | 365 | test_syntax_error ("%s: unary operator expected", argv[pos]); |
726f6388 JA |
366 | } |
367 | else | |
368 | { | |
ccc6cda3 | 369 | value = argv[pos][0] != '\0'; |
726f6388 JA |
370 | advance (0); |
371 | } | |
372 | ||
373 | return (value); | |
374 | } | |
375 | ||
376 | static int | |
ccc6cda3 JA |
377 | filecomp (s, t, op) |
378 | char *s, *t; | |
379 | int op; | |
726f6388 | 380 | { |
ccc6cda3 | 381 | struct stat st1, st2; |
726f6388 | 382 | |
ccc6cda3 JA |
383 | if (test_stat (s, &st1) < 0 || test_stat (t, &st2) < 0) |
384 | return (FALSE); | |
385 | switch (op) | |
726f6388 | 386 | { |
ccc6cda3 JA |
387 | case OT: return (st1.st_mtime < st2.st_mtime); |
388 | case NT: return (st1.st_mtime > st2.st_mtime); | |
389 | case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino)); | |
390 | } | |
391 | return (FALSE); | |
392 | } | |
726f6388 | 393 | |
ccc6cda3 | 394 | static int |
cce855bc | 395 | arithcomp (s, t, op, flags) |
ccc6cda3 | 396 | char *s, *t; |
cce855bc | 397 | int op, flags; |
ccc6cda3 JA |
398 | { |
399 | long l, r; | |
cce855bc JA |
400 | int expok; |
401 | ||
402 | if (flags & TEST_ARITHEXP) | |
403 | { | |
404 | l = evalexp (s, &expok); | |
405 | if (expok == 0) | |
406 | return (FALSE); /* should probably longjmp here */ | |
407 | r = evalexp (t, &expok); | |
408 | if (expok == 0) | |
409 | return (FALSE); /* ditto */ | |
410 | } | |
411 | else | |
412 | { | |
413 | if (legal_number (s, &l) == 0) | |
414 | integer_expected_error (s); | |
415 | if (legal_number (t, &r) == 0) | |
416 | integer_expected_error (t); | |
417 | } | |
726f6388 | 418 | |
ccc6cda3 JA |
419 | switch (op) |
420 | { | |
421 | case EQ: return (l == r); | |
422 | case NE: return (l != r); | |
423 | case LT: return (l < r); | |
424 | case GT: return (l > r); | |
425 | case LE: return (l <= r); | |
426 | case GE: return (l >= r); | |
726f6388 | 427 | } |
cce855bc | 428 | |
ccc6cda3 JA |
429 | return (FALSE); |
430 | } | |
431 | ||
ccc6cda3 JA |
432 | static int |
433 | patcomp (string, pat, op) | |
434 | char *string, *pat; | |
435 | int op; | |
436 | { | |
437 | int m; | |
438 | ||
cce855bc JA |
439 | m = fnmatch (pat, string, FNMATCH_EXTFLAG); |
440 | return ((op == EQ) ? (m == 0) : (m != 0)); | |
441 | } | |
442 | ||
443 | int | |
444 | binary_test (op, arg1, arg2, flags) | |
445 | char *op, *arg1, *arg2; | |
446 | int flags; | |
447 | { | |
448 | int patmatch; | |
449 | ||
450 | patmatch = (flags & TEST_PATMATCH); | |
451 | ||
452 | if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0'))) | |
453 | return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2)); | |
454 | ||
455 | else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0') | |
456 | return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0)); | |
457 | ||
458 | else if (op[0] == '!' && op[1] == '=' && op[2] == '\0') | |
459 | return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0)); | |
460 | ||
461 | else if (op[2] == 't') | |
462 | { | |
463 | switch (op[1]) | |
464 | { | |
465 | case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */ | |
466 | case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */ | |
467 | case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */ | |
468 | case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */ | |
469 | } | |
470 | } | |
471 | else if (op[1] == 'e') | |
726f6388 | 472 | { |
cce855bc JA |
473 | switch (op[2]) |
474 | { | |
475 | case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */ | |
476 | case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */ | |
477 | } | |
478 | } | |
479 | else if (op[2] == 'e') | |
480 | { | |
481 | switch (op[1]) | |
482 | { | |
483 | case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */ | |
484 | case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */ | |
485 | case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */ | |
486 | } | |
726f6388 | 487 | } |
cce855bc JA |
488 | |
489 | return (FALSE); /* should never get here */ | |
ccc6cda3 | 490 | } |
cce855bc | 491 | |
726f6388 | 492 | |
ccc6cda3 JA |
493 | static int |
494 | binary_operator () | |
495 | { | |
496 | int value; | |
497 | char *w; | |
498 | ||
499 | w = argv[pos + 1]; | |
cce855bc JA |
500 | if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */ |
501 | ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */ | |
502 | (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */ | |
726f6388 | 503 | { |
cce855bc | 504 | value = binary_test (w, argv[pos], argv[pos + 2], 0); |
ccc6cda3 JA |
505 | pos += 3; |
506 | return (value); | |
507 | } | |
cce855bc | 508 | |
ccc6cda3 JA |
509 | #if defined (PATTERN_MATCHING) |
510 | if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0') | |
511 | { | |
512 | value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE); | |
513 | pos += 3; | |
514 | return (value); | |
515 | } | |
516 | #endif | |
ccc6cda3 | 517 | |
cce855bc | 518 | if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0) |
ccc6cda3 JA |
519 | { |
520 | test_syntax_error ("%s: binary operator expected", w); | |
521 | /* NOTREACHED */ | |
522 | return (FALSE); | |
726f6388 | 523 | } |
726f6388 | 524 | |
cce855bc | 525 | value = binary_test (w, argv[pos], argv[pos + 2], 0); |
ccc6cda3 JA |
526 | pos += 3; |
527 | return value; | |
726f6388 JA |
528 | } |
529 | ||
530 | static int | |
531 | unary_operator () | |
532 | { | |
cce855bc | 533 | char *op, *arg; |
ccc6cda3 | 534 | long r; |
726f6388 | 535 | |
cce855bc JA |
536 | op = argv[pos]; |
537 | if (test_unop (op) == 0) | |
538 | return (FALSE); | |
539 | ||
540 | /* the only tricky case is `-t', which may or may not take an argument. */ | |
541 | if (op[1] == 't') | |
726f6388 | 542 | { |
cce855bc JA |
543 | advance (0); |
544 | if (pos < argc && legal_number (argv[pos], &r)) | |
545 | { | |
546 | advance (0); | |
547 | return (unary_test (op, argv[pos - 1])); | |
548 | } | |
549 | else | |
550 | return (unary_test (op, "1")); | |
551 | } | |
726f6388 | 552 | |
cce855bc JA |
553 | /* All of the unary operators take an argument, so we first call |
554 | unary_advance (), which checks to make sure that there is an | |
555 | argument, and then advances pos right past it. This means that | |
556 | pos - 1 is the location of the argument. */ | |
557 | unary_advance (); | |
558 | return (unary_test (op, argv[pos - 1])); | |
559 | } | |
726f6388 | 560 | |
cce855bc JA |
561 | int |
562 | unary_test (op, arg) | |
563 | char *op, *arg; | |
564 | { | |
565 | long r; | |
566 | struct stat stat_buf; | |
567 | ||
568 | switch (op[1]) | |
569 | { | |
726f6388 JA |
570 | case 'a': /* file exists in the file system? */ |
571 | case 'e': | |
cce855bc | 572 | return (test_stat (arg, &stat_buf) == 0); |
726f6388 JA |
573 | |
574 | case 'r': /* file is readable? */ | |
cce855bc | 575 | return (EACCESS (arg, R_OK) == 0); |
726f6388 JA |
576 | |
577 | case 'w': /* File is writeable? */ | |
cce855bc | 578 | return (EACCESS (arg, W_OK) == 0); |
726f6388 JA |
579 | |
580 | case 'x': /* File is executable? */ | |
cce855bc | 581 | return (EACCESS (arg, X_OK) == 0); |
726f6388 JA |
582 | |
583 | case 'O': /* File is owned by you? */ | |
cce855bc | 584 | return (test_stat (arg, &stat_buf) == 0 && |
d166f048 | 585 | (uid_t) current_user.euid == (uid_t) stat_buf.st_uid); |
726f6388 JA |
586 | |
587 | case 'G': /* File is owned by your group? */ | |
cce855bc | 588 | return (test_stat (arg, &stat_buf) == 0 && |
d166f048 | 589 | (gid_t) current_user.egid == (gid_t) stat_buf.st_gid); |
726f6388 | 590 | |
cce855bc JA |
591 | case 'N': |
592 | return (test_stat (arg, &stat_buf) == 0 && | |
593 | stat_buf.st_atime <= stat_buf.st_mtime); | |
594 | ||
726f6388 | 595 | case 'f': /* File is a file? */ |
cce855bc | 596 | if (test_stat (arg, &stat_buf) < 0) |
726f6388 JA |
597 | return (FALSE); |
598 | ||
ccc6cda3 | 599 | /* -f is true if the given file exists and is a regular file. */ |
726f6388 | 600 | #if defined (S_IFMT) |
ccc6cda3 | 601 | return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0); |
726f6388 | 602 | #else |
ccc6cda3 | 603 | return (S_ISREG (stat_buf.st_mode)); |
726f6388 JA |
604 | #endif /* !S_IFMT */ |
605 | ||
606 | case 'd': /* File is a directory? */ | |
cce855bc | 607 | return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode))); |
726f6388 JA |
608 | |
609 | case 's': /* File has something in it? */ | |
cce855bc | 610 | return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0); |
726f6388 JA |
611 | |
612 | case 'S': /* File is a socket? */ | |
613 | #if !defined (S_ISSOCK) | |
614 | return (FALSE); | |
615 | #else | |
cce855bc | 616 | return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode)); |
ccc6cda3 | 617 | #endif /* S_ISSOCK */ |
726f6388 JA |
618 | |
619 | case 'c': /* File is character special? */ | |
cce855bc | 620 | return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode)); |
726f6388 JA |
621 | |
622 | case 'b': /* File is block special? */ | |
cce855bc | 623 | return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode)); |
726f6388 JA |
624 | |
625 | case 'p': /* File is a named pipe? */ | |
726f6388 JA |
626 | #ifndef S_ISFIFO |
627 | return (FALSE); | |
628 | #else | |
cce855bc | 629 | return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode)); |
ccc6cda3 | 630 | #endif /* S_ISFIFO */ |
726f6388 JA |
631 | |
632 | case 'L': /* Same as -h */ | |
726f6388 | 633 | case 'h': /* File is a symbolic link? */ |
ccc6cda3 | 634 | #if !defined (S_ISLNK) || !defined (HAVE_LSTAT) |
726f6388 JA |
635 | return (FALSE); |
636 | #else | |
cce855bc JA |
637 | return ((arg[0] != '\0') && |
638 | (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode)); | |
ccc6cda3 | 639 | #endif /* S_IFLNK && HAVE_LSTAT */ |
726f6388 JA |
640 | |
641 | case 'u': /* File is setuid? */ | |
cce855bc | 642 | return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0); |
726f6388 JA |
643 | |
644 | case 'g': /* File is setgid? */ | |
cce855bc | 645 | return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0); |
726f6388 JA |
646 | |
647 | case 'k': /* File has sticky bit set? */ | |
726f6388 JA |
648 | #if !defined (S_ISVTX) |
649 | /* This is not Posix, and is not defined on some Posix systems. */ | |
650 | return (FALSE); | |
651 | #else | |
cce855bc | 652 | return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0); |
726f6388 JA |
653 | #endif |
654 | ||
cce855bc JA |
655 | case 't': /* File fd is a terminal? */ |
656 | if (legal_number (arg, &r) == 0) | |
657 | return (FALSE); | |
658 | return (isatty ((int)r)); | |
726f6388 JA |
659 | |
660 | case 'n': /* True if arg has some length. */ | |
cce855bc | 661 | return (arg[0] != '\0'); |
726f6388 JA |
662 | |
663 | case 'z': /* True if arg has no length. */ | |
cce855bc | 664 | return (arg[0] == '\0'); |
726f6388 | 665 | |
cce855bc JA |
666 | case 'o': /* True if option `arg' is set. */ |
667 | return (minus_o_option_value (arg) == 1); | |
726f6388 | 668 | } |
726f6388 JA |
669 | } |
670 | ||
cce855bc JA |
671 | /* Return TRUE if OP is one of the test command's binary operators. */ |
672 | int | |
673 | test_binop (op) | |
674 | char *op; | |
726f6388 | 675 | { |
cce855bc | 676 | if (op[0] == '=' && op[1] == '\0') |
ccc6cda3 | 677 | return (1); /* '=' */ |
cce855bc | 678 | else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */ |
ccc6cda3 | 679 | return (1); |
cce855bc | 680 | else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0') |
ccc6cda3 JA |
681 | return (1); /* `==' and `!=' */ |
682 | #if defined (PATTERN_MATCHING) | |
cce855bc | 683 | else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!')) |
ccc6cda3 JA |
684 | return (1); |
685 | #endif | |
cce855bc | 686 | else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0') |
ccc6cda3 JA |
687 | return (0); |
688 | else | |
689 | { | |
cce855bc JA |
690 | if (op[2] == 't') |
691 | switch (op[1]) | |
ccc6cda3 | 692 | { |
cce855bc JA |
693 | case 'n': /* -nt */ |
694 | case 'o': /* -ot */ | |
695 | case 'l': /* -lt */ | |
696 | case 'g': /* -gt */ | |
697 | return (1); | |
698 | default: | |
699 | return (0); | |
ccc6cda3 | 700 | } |
cce855bc JA |
701 | else if (op[1] == 'e') |
702 | switch (op[2]) | |
ccc6cda3 | 703 | { |
cce855bc JA |
704 | case 'q': /* -eq */ |
705 | case 'f': /* -ef */ | |
706 | return (1); | |
707 | default: | |
708 | return (0); | |
ccc6cda3 | 709 | } |
cce855bc JA |
710 | else if (op[2] == 'e') |
711 | switch (op[1]) | |
ccc6cda3 | 712 | { |
cce855bc JA |
713 | case 'n': /* -ne */ |
714 | case 'g': /* -ge */ | |
715 | case 'l': /* -le */ | |
716 | return (1); | |
717 | default: | |
718 | return (0); | |
ccc6cda3 JA |
719 | } |
720 | else | |
721 | return (0); | |
722 | } | |
726f6388 JA |
723 | } |
724 | ||
725 | /* Return non-zero if OP is one of the test command's unary operators. */ | |
cce855bc JA |
726 | int |
727 | test_unop (op) | |
728 | char *op; | |
726f6388 | 729 | { |
cce855bc JA |
730 | if (op[0] != '-') |
731 | return (0); | |
732 | ||
733 | switch (op[1]) | |
ccc6cda3 JA |
734 | { |
735 | case 'a': case 'b': case 'c': case 'd': case 'e': | |
736 | case 'f': case 'g': case 'h': case 'k': case 'n': | |
cce855bc JA |
737 | case 'o': case 'p': case 'r': case 's': case 't': |
738 | case 'u': case 'w': case 'x': case 'z': | |
739 | case 'G': case 'L': case 'O': case 'S': case 'N': | |
ccc6cda3 JA |
740 | return (1); |
741 | } | |
cce855bc | 742 | |
ccc6cda3 | 743 | return (0); |
726f6388 JA |
744 | } |
745 | ||
746 | static int | |
747 | two_arguments () | |
748 | { | |
ccc6cda3 JA |
749 | if (argv[pos][0] == '!' && argv[pos][1] == '\0') |
750 | return (argv[pos + 1][0] == '\0'); | |
751 | else if (argv[pos][0] == '-' && argv[pos][2] == '\0') | |
726f6388 | 752 | { |
cce855bc | 753 | if (test_unop (argv[pos])) |
ccc6cda3 | 754 | return (unary_operator ()); |
726f6388 | 755 | else |
ccc6cda3 | 756 | test_syntax_error ("%s: unary operator expected", argv[pos]); |
726f6388 JA |
757 | } |
758 | else | |
ccc6cda3 | 759 | test_syntax_error ("%s: unary operator expected", argv[pos]); |
726f6388 | 760 | |
ccc6cda3 | 761 | return (0); |
726f6388 JA |
762 | } |
763 | ||
ccc6cda3 JA |
764 | #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o')) |
765 | ||
766 | #define ONE_ARG_TEST(s) ((s)[0] != '\0') | |
767 | ||
726f6388 JA |
768 | static int |
769 | three_arguments () | |
770 | { | |
771 | int value; | |
772 | ||
cce855bc | 773 | if (test_binop (argv[pos+1])) |
ccc6cda3 JA |
774 | { |
775 | value = binary_operator (); | |
776 | pos = argc; | |
777 | } | |
778 | else if (ANDOR (argv[pos+1])) | |
779 | { | |
780 | if (argv[pos+1][1] == 'a') | |
781 | value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]); | |
782 | else | |
783 | value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]); | |
784 | pos = argc; | |
785 | } | |
cce855bc | 786 | else if (argv[pos][0] == '!' && argv[pos][1] == '\0') |
726f6388 JA |
787 | { |
788 | advance (1); | |
789 | value = !two_arguments (); | |
790 | } | |
ccc6cda3 | 791 | else if (argv[pos][0] == '(' && argv[pos+2][0] == ')') |
726f6388 | 792 | { |
ccc6cda3 | 793 | value = ONE_ARG_TEST(argv[pos+1]); |
726f6388 JA |
794 | pos = argc; |
795 | } | |
726f6388 | 796 | else |
ccc6cda3 JA |
797 | test_syntax_error ("%s: binary operator expected", argv[pos+1]); |
798 | ||
726f6388 JA |
799 | return (value); |
800 | } | |
801 | ||
802 | /* This is an implementation of a Posix.2 proposal by David Korn. */ | |
803 | static int | |
804 | posixtest () | |
805 | { | |
806 | int value; | |
807 | ||
808 | switch (argc - 1) /* one extra passed in */ | |
809 | { | |
810 | case 0: | |
811 | value = FALSE; | |
812 | pos = argc; | |
813 | break; | |
814 | ||
815 | case 1: | |
ccc6cda3 | 816 | value = ONE_ARG_TEST(argv[1]); |
726f6388 JA |
817 | pos = argc; |
818 | break; | |
819 | ||
820 | case 2: | |
821 | value = two_arguments (); | |
822 | pos = argc; | |
823 | break; | |
824 | ||
825 | case 3: | |
826 | value = three_arguments (); | |
827 | break; | |
828 | ||
829 | case 4: | |
ccc6cda3 | 830 | if (argv[pos][0] == '!' && argv[pos][1] == '\0') |
726f6388 JA |
831 | { |
832 | advance (1); | |
833 | value = !three_arguments (); | |
834 | break; | |
835 | } | |
836 | /* FALLTHROUGH */ | |
726f6388 JA |
837 | default: |
838 | value = expr (); | |
839 | } | |
840 | ||
841 | return (value); | |
842 | } | |
843 | ||
844 | /* | |
845 | * [: | |
846 | * '[' expr ']' | |
847 | * test: | |
848 | * test expr | |
849 | */ | |
850 | int | |
d166f048 | 851 | test_command (margc, margv) |
726f6388 JA |
852 | int margc; |
853 | char **margv; | |
854 | { | |
855 | int value; | |
856 | ||
726f6388 JA |
857 | int code; |
858 | ||
859 | code = setjmp (test_exit_buf); | |
860 | ||
861 | if (code) | |
862 | return (test_error_return); | |
726f6388 JA |
863 | |
864 | argv = margv; | |
865 | ||
ccc6cda3 | 866 | if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0') |
726f6388 JA |
867 | { |
868 | --margc; | |
869 | ||
870 | if (margc < 2) | |
871 | test_exit (SHELL_BOOLEAN (FALSE)); | |
872 | ||
873 | if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1])) | |
ccc6cda3 | 874 | test_syntax_error ("missing `]'", (char *)NULL); |
726f6388 JA |
875 | } |
876 | ||
877 | argc = margc; | |
878 | pos = 1; | |
879 | ||
880 | if (pos >= argc) | |
881 | test_exit (SHELL_BOOLEAN (FALSE)); | |
882 | ||
883 | noeval = 0; | |
884 | value = posixtest (); | |
885 | ||
886 | if (pos != argc) | |
ccc6cda3 | 887 | test_syntax_error ("too many arguments", (char *)NULL); |
726f6388 JA |
888 | |
889 | test_exit (SHELL_BOOLEAN (value)); | |
890 | } |