blob: ceb98c7b9a3db1bef21c3bd32b5b5a6950c5cdce [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023/* 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
Jari Aalto726f6381996-08-26 18:22:31 +000031#include <stdio.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000032
Jari Aaltod166f041997-06-05 14:59:13 +000033#include "bashtypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000034
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
Jari Aaltoe8ce7751997-09-22 20:22:27 +000045#include <errno.h>
46#if !defined (errno)
47extern int errno;
48#endif /* !errno */
49
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050#if !defined (_POSIX_VERSION)
51# include <sys/file.h>
52#endif /* !_POSIX_VERSION */
53#include "posixstat.h"
54#include "filecntl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000055
Jari Aaltod166f041997-06-05 14:59:13 +000056#include "shell.h"
57#include "builtins/common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000058
59#if !defined (STRLEN)
60# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
61#endif
62
Jari Aalto726f6381996-08-26 18:22:31 +000063#if !defined (STREQ)
64# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
65#endif /* !STREQ */
66
67#if !defined (member)
68# define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0)
69#endif /* !member */
70
Jari Aalto726f6381996-08-26 18:22:31 +000071#if !defined (R_OK)
72#define R_OK 4
73#define W_OK 2
74#define X_OK 1
75#define F_OK 0
76#endif /* R_OK */
77
Jari Aaltoccc6cda1996-12-23 17:02:34 +000078#define EQ 0
79#define NE 1
80#define LT 2
81#define GT 3
82#define LE 4
83#define GE 5
84
85#define NT 0
86#define OT 1
87#define EF 2
88
Jari Aalto726f6381996-08-26 18:22:31 +000089/* The following few defines control the truth and false output of each stage.
90 TRUE and FALSE are what we use to compute the final output value.
91 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000092 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
Jari Aalto726f6381996-08-26 18:22:31 +000093#define TRUE 1
94#define FALSE 0
95#define SHELL_BOOLEAN(value) (!(value))
Jari Aalto726f6381996-08-26 18:22:31 +000096
Jari Aaltoccc6cda1996-12-23 17:02:34 +000097static procenv_t test_exit_buf;
98static int test_error_return;
Jari Aaltod166f041997-06-05 14:59:13 +000099#define test_exit(val) \
Jari Aalto726f6381996-08-26 18:22:31 +0000100 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000101
102#if defined (AFS)
103 /* We have to use access(2) for machines running AFS, because it's
104 not a Unix file system. This may produce incorrect answers for
105 non-AFS files. I hate AFS. */
106# define EACCESS(path, mode) access(path, mode)
107#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108# define EACCESS(path, mode) test_eaccess(path, mode)
Jari Aalto726f6381996-08-26 18:22:31 +0000109#endif /* AFS */
110
111static int pos; /* The offset of the current argument in ARGV. */
112static int argc; /* The number of arguments present in ARGV. */
113static char **argv; /* The argument list. */
114static int noeval;
115
Jari Aalto726f6381996-08-26 18:22:31 +0000116static int unop ();
117static int binop ();
118static int unary_operator ();
119static int binary_operator ();
120static int two_arguments ();
121static int three_arguments ();
122static int posixtest ();
123
124static int expr ();
125static int term ();
126static int and ();
127static int or ();
128
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000129static void beyond ();
130
Jari Aalto726f6381996-08-26 18:22:31 +0000131static void
132test_syntax_error (format, arg)
133 char *format, *arg;
134{
Jari Aalto726f6381996-08-26 18:22:31 +0000135 extern int interactive_shell;
136 extern char *get_name_for_error ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000137 if (interactive_shell == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000138 fprintf (stderr, "%s: ", get_name_for_error ());
Jari Aalto726f6381996-08-26 18:22:31 +0000139 fprintf (stderr, "%s: ", argv[0]);
140 fprintf (stderr, format, arg);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000141 fprintf (stderr, "\n");
Jari Aalto726f6381996-08-26 18:22:31 +0000142 fflush (stderr);
143 test_exit (SHELL_BOOLEAN (FALSE));
144}
145
146/* A wrapper for stat () which disallows pathnames that are empty strings
147 and handles /dev/fd emulation on systems that don't have it. */
148static int
149test_stat (path, finfo)
150 char *path;
151 struct stat *finfo;
152{
153 if (*path == '\0')
154 {
155 errno = ENOENT;
156 return (-1);
157 }
Jari Aalto726f6381996-08-26 18:22:31 +0000158 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
159 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000160#if !defined (HAVE_DEV_FD)
161 long fd;
Jari Aaltod166f041997-06-05 14:59:13 +0000162 if (legal_number (path + 8, &fd))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000163 return (fstat ((int)fd, finfo));
Jari Aalto726f6381996-08-26 18:22:31 +0000164 else
165 {
166 errno = EBADF;
167 return (-1);
168 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000169#else
170 /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
171 trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
172 On most systems, with the notable exception of linux, this is
173 effectively a no-op. */
174 char pbuf[32];
175 strcpy (pbuf, DEV_FD_PREFIX);
176 strcat (pbuf, path + 8);
177 return (stat (pbuf, finfo));
Jari Aalto726f6381996-08-26 18:22:31 +0000178#endif /* !HAVE_DEV_FD */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000179 }
Jari Aalto726f6381996-08-26 18:22:31 +0000180 return (stat (path, finfo));
181}
182
183/* Do the same thing access(2) does, but use the effective uid and gid,
184 and don't make the mistake of telling root that any file is
185 executable. */
186static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000187test_eaccess (path, mode)
Jari Aalto726f6381996-08-26 18:22:31 +0000188 char *path;
189 int mode;
190{
191 struct stat st;
Jari Aalto726f6381996-08-26 18:22:31 +0000192
193 if (test_stat (path, &st) < 0)
194 return (-1);
195
Jari Aaltod166f041997-06-05 14:59:13 +0000196 if (current_user.euid == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000197 {
198 /* Root can read or write any file. */
199 if (mode != X_OK)
200 return (0);
201
202 /* Root can execute any file that has any one of the execute
203 bits set. */
204 if (st.st_mode & S_IXUGO)
205 return (0);
206 }
207
Jari Aaltod166f041997-06-05 14:59:13 +0000208 if (st.st_uid == current_user.euid) /* owner */
Jari Aalto726f6381996-08-26 18:22:31 +0000209 mode <<= 6;
210 else if (group_member (st.st_gid))
211 mode <<= 3;
212
213 if (st.st_mode & mode)
214 return (0);
215
216 return (-1);
217}
218
Jari Aalto726f6381996-08-26 18:22:31 +0000219/* Increment our position in the argument list. Check that we're not
220 past the end of the argument list. This check is supressed if the
221 argument is FALSE. Made a macro for efficiency. */
Jari Aalto726f6381996-08-26 18:22:31 +0000222#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000223#define unary_advance() do { advance (1); ++pos; } while (0)
224
225/*
226 * beyond - call when we're beyond the end of the argument list (an
227 * error condition)
228 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000229static void
Jari Aalto726f6381996-08-26 18:22:31 +0000230beyond ()
231{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000232 test_syntax_error ("argument expected", (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000233}
234
235/* Syntax error for when an integer argument was expected, but
236 something else was found. */
237static void
238integer_expected_error (pch)
239 char *pch;
240{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000241 test_syntax_error ("%s: integer expression expected", pch);
Jari Aalto726f6381996-08-26 18:22:31 +0000242}
243
Jari Aalto726f6381996-08-26 18:22:31 +0000244/*
245 * term - parse a term and return 1 or 0 depending on whether the term
246 * evaluates to true or false, respectively.
247 *
248 * term ::=
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000249 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'p'|'r'|'s'|'u'|'w'|'x') filename
250 * '-'('G'|'L'|'O'|'S') filename
251 * '-t' [int]
Jari Aalto726f6381996-08-26 18:22:31 +0000252 * '-'('z'|'n') string
253 * string
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000254 * string ('!='|'='|'==') string
Jari Aalto726f6381996-08-26 18:22:31 +0000255 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
256 * file '-'(nt|ot|ef) file
257 * '(' <expr> ')'
258 * int ::=
Jari Aalto726f6381996-08-26 18:22:31 +0000259 * positive and negative integers
260 */
261static int
262term ()
263{
264 int value;
265
266 if (pos >= argc)
267 beyond ();
268
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269 /* Deal with leading `not's. */
270 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000271 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000272 value = 0;
273 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000274 {
275 advance (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000276 value = 1 - value;
Jari Aalto726f6381996-08-26 18:22:31 +0000277 }
278
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000279 return (value ? !term() : term());
Jari Aalto726f6381996-08-26 18:22:31 +0000280 }
281
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000282 /* A paren-bracketed argument. */
283 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000284 {
285 advance (1);
286 value = expr ();
287 if (argv[pos] == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000288 test_syntax_error ("`)' expected", (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000289 else if (argv[pos][0] != ')' || argv[pos][1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000290 test_syntax_error ("`)' expected, found %s", argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000291 advance (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000292 return (value);
Jari Aalto726f6381996-08-26 18:22:31 +0000293 }
294
Jari Aaltod166f041997-06-05 14:59:13 +0000295#if 1
Jari Aalto726f6381996-08-26 18:22:31 +0000296 /* are there enough arguments left that this could be dyadic? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000297 if ((pos + 3 <= argc) && binop (argv[pos + 1]))
Jari Aalto726f6381996-08-26 18:22:31 +0000298 value = binary_operator ();
Jari Aaltod166f041997-06-05 14:59:13 +0000299#else
300 /* If this is supposed to be a binary operator, make sure there are
301 enough arguments and fail if there are not. */
302 if ((pos + 1 < argc) && binop (argv[pos+1]))
303 {
304 if (pos + 3 <= argc)
305 value = binary_operator ();
306 else
307 beyond ();
308 }
309#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000310
311 /* Might be a switch type argument */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000312 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000313 {
314 if (unop (argv[pos][1]))
315 value = unary_operator ();
316 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000317 test_syntax_error ("%s: unary operator expected", argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000318 }
319 else
320 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000321 value = argv[pos][0] != '\0';
Jari Aalto726f6381996-08-26 18:22:31 +0000322 advance (0);
323 }
324
325 return (value);
326}
327
328static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000329filecomp (s, t, op)
330 char *s, *t;
331 int op;
Jari Aalto726f6381996-08-26 18:22:31 +0000332{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000333 struct stat st1, st2;
Jari Aalto726f6381996-08-26 18:22:31 +0000334
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000335 if (test_stat (s, &st1) < 0 || test_stat (t, &st2) < 0)
336 return (FALSE);
337 switch (op)
Jari Aalto726f6381996-08-26 18:22:31 +0000338 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000339 case OT: return (st1.st_mtime < st2.st_mtime);
340 case NT: return (st1.st_mtime > st2.st_mtime);
341 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
Jari Aalto726f6381996-08-26 18:22:31 +0000342 }
343 return (FALSE);
344}
345
346static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000347arithcomp (s, t, op)
348 char *s, *t;
349 int op;
350{
351 long l, r;
352
Jari Aaltod166f041997-06-05 14:59:13 +0000353 if (legal_number (s, &l) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000354 integer_expected_error (s);
Jari Aaltod166f041997-06-05 14:59:13 +0000355 if (legal_number (t, &r) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000356 integer_expected_error (t);
357 switch (op)
358 {
359 case EQ: return (l == r);
360 case NE: return (l != r);
361 case LT: return (l < r);
362 case GT: return (l > r);
363 case LE: return (l <= r);
364 case GE: return (l >= r);
365 }
366 return (FALSE);
367}
368
369#if defined (PATTERN_MATCHING)
370static int
371patcomp (string, pat, op)
372 char *string, *pat;
373 int op;
374{
375 int m;
376
377 m = fnmatch (pat, string, 0);
378 switch (op)
379 {
380 case EQ: return (m == 0);
381 case NE: return (m != 0);
382 }
383}
384#endif /* PATTERN_MATCHING */
385
386static int
387binary_operator ()
388{
389 int value;
390 char *w;
391
392 w = argv[pos + 1];
393 if (w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0')))
394 {
395 value = STREQ (argv[pos], argv[pos + 2]);
396 pos += 3;
397 return (value);
398 }
399 if ((w[0] == '>' || w[0] == '<') && w[1] == '\0')
400 {
401 value = (w[0] == '>') ? strcmp (argv[pos], argv[pos + 2]) > 0
402 : strcmp (argv[pos], argv[pos + 2]) < 0;
403 pos += 3;
404 return (value);
405 }
406#if defined (PATTERN_MATCHING)
407 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
408 {
409 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
410 pos += 3;
411 return (value);
412 }
413#endif
414 if (w[0] == '!' && w[1] == '=' && w[2] == '\0')
415 {
416 value = STREQ (argv[pos], argv[pos + 2]) == 0;
417 pos += 3;
418 return (value);
419 }
420
421 if (w[0] != '-' || w[3] != '\0')
422 {
423 test_syntax_error ("%s: binary operator expected", w);
424 /* NOTREACHED */
425 return (FALSE);
426 }
427
428 w++;
429 if (w[1] == 't')
430 {
431 switch (w[0])
432 {
433 case 'n': value = filecomp (argv[pos], argv[pos + 2], NT); break;
434 case 'o': value = filecomp (argv[pos], argv[pos + 2], OT); break;
435 case 'l': value = arithcomp (argv[pos], argv[pos + 2], LT); break;
436 case 'g': value = arithcomp (argv[pos], argv[pos + 2], GT); break;
437 default: test_syntax_error ("-%s: binary operator expected", w);
438 }
439 }
440 else if (w[0] == 'e')
441 {
442 switch (w[1])
443 {
444 case 'q': value = arithcomp (argv[pos], argv[pos + 2], EQ); break;
445 case 'f': value = filecomp (argv[pos], argv[pos + 2], EF); break;
446 default: test_syntax_error ("-%s: binary operator expected", w);
447 }
448 }
449 else if (w[1] == 'e')
450 {
451 switch (w[0])
452 {
453 case 'n': value = arithcomp (argv[pos], argv[pos + 2], NE); break;
454 case 'g': value = arithcomp (argv[pos], argv[pos + 2], GE); break;
455 case 'l': value = arithcomp (argv[pos], argv[pos + 2], LE); break;
456 default: test_syntax_error ("-%s: binary operator expected", w);
457 }
458 }
459 else
460 test_syntax_error ("-%s: binary operator expected", w);
461
462 pos += 3;
463 return value;
464}
465
466static int
Jari Aalto726f6381996-08-26 18:22:31 +0000467unary_operator ()
468{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000469 long r;
Jari Aalto726f6381996-08-26 18:22:31 +0000470 struct stat stat_buf;
471
472 switch (argv[pos][1])
473 {
474 default:
475 return (FALSE);
476
477 /* All of the following unary operators use unary_advance (), which
478 checks to make sure that there is an argument, and then advances
479 pos right past it. This means that pos - 1 is the location of the
480 argument. */
481
482 case 'a': /* file exists in the file system? */
483 case 'e':
484 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000485 return (test_stat (argv[pos - 1], &stat_buf) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000486
487 case 'r': /* file is readable? */
488 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000489 return (EACCESS (argv[pos - 1], R_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000490
491 case 'w': /* File is writeable? */
492 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000493 return (EACCESS (argv[pos - 1], W_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000494
495 case 'x': /* File is executable? */
496 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000497 return (EACCESS (argv[pos - 1], X_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000498
499 case 'O': /* File is owned by you? */
500 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000501 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000502 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
Jari Aalto726f6381996-08-26 18:22:31 +0000503
504 case 'G': /* File is owned by your group? */
505 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000506 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000507 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
Jari Aalto726f6381996-08-26 18:22:31 +0000508
509 case 'f': /* File is a file? */
510 unary_advance ();
511 if (test_stat (argv[pos - 1], &stat_buf) < 0)
512 return (FALSE);
513
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000514 /* -f is true if the given file exists and is a regular file. */
Jari Aalto726f6381996-08-26 18:22:31 +0000515#if defined (S_IFMT)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000516 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000517#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000518 return (S_ISREG (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000519#endif /* !S_IFMT */
520
521 case 'd': /* File is a directory? */
522 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000523 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
524 (S_ISDIR (stat_buf.st_mode)));
Jari Aalto726f6381996-08-26 18:22:31 +0000525
526 case 's': /* File has something in it? */
527 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000528 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
529 stat_buf.st_size > (off_t) 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000530
531 case 'S': /* File is a socket? */
532#if !defined (S_ISSOCK)
533 return (FALSE);
534#else
535 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000536 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
537 S_ISSOCK (stat_buf.st_mode));
538#endif /* S_ISSOCK */
Jari Aalto726f6381996-08-26 18:22:31 +0000539
540 case 'c': /* File is character special? */
541 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000542 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
543 S_ISCHR (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000544
545 case 'b': /* File is block special? */
546 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000547 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
548 S_ISBLK (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000549
550 case 'p': /* File is a named pipe? */
551 unary_advance ();
552#ifndef S_ISFIFO
553 return (FALSE);
554#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000555 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
556 S_ISFIFO (stat_buf.st_mode));
557#endif /* S_ISFIFO */
Jari Aalto726f6381996-08-26 18:22:31 +0000558
559 case 'L': /* Same as -h */
Jari Aalto726f6381996-08-26 18:22:31 +0000560 case 'h': /* File is a symbolic link? */
561 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000562#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
Jari Aalto726f6381996-08-26 18:22:31 +0000563 return (FALSE);
564#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000565 return ((argv[pos - 1][0] != '\0') &&
566 (lstat (argv[pos - 1], &stat_buf) == 0) &&
567 S_ISLNK (stat_buf.st_mode));
568#endif /* S_IFLNK && HAVE_LSTAT */
Jari Aalto726f6381996-08-26 18:22:31 +0000569
570 case 'u': /* File is setuid? */
571 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000572 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
573 (stat_buf.st_mode & S_ISUID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000574
575 case 'g': /* File is setgid? */
576 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000577 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
578 (stat_buf.st_mode & S_ISGID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000579
580 case 'k': /* File has sticky bit set? */
581 unary_advance ();
Jari Aalto726f6381996-08-26 18:22:31 +0000582#if !defined (S_ISVTX)
583 /* This is not Posix, and is not defined on some Posix systems. */
584 return (FALSE);
585#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000586 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
587 (stat_buf.st_mode & S_ISVTX) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000588#endif
589
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000590 case 't': /* File fd is a terminal? fd defaults to stdout. */
Jari Aalto726f6381996-08-26 18:22:31 +0000591 advance (0);
Jari Aaltod166f041997-06-05 14:59:13 +0000592 if (pos < argc && legal_number (argv[pos], &r))
Jari Aalto726f6381996-08-26 18:22:31 +0000593 {
594 advance (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000595 return (isatty ((int)r));
Jari Aalto726f6381996-08-26 18:22:31 +0000596 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000597 return (isatty (1));
Jari Aalto726f6381996-08-26 18:22:31 +0000598
599 case 'n': /* True if arg has some length. */
600 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000601 return (argv[pos - 1][0] != '\0');
Jari Aalto726f6381996-08-26 18:22:31 +0000602
603 case 'z': /* True if arg has no length. */
604 unary_advance ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000605 return (argv[pos - 1][0] == '\0');
606
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000607 case 'o':
608 unary_advance ();
609 return (minus_o_option_value (argv[pos - 1]) == 1);
Jari Aalto726f6381996-08-26 18:22:31 +0000610 }
611}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000612
Jari Aalto726f6381996-08-26 18:22:31 +0000613/*
614 * and:
615 * term
616 * term '-a' and
617 */
618static int
619and ()
620{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000621 int value, v2;
Jari Aalto726f6381996-08-26 18:22:31 +0000622
623 value = term ();
624 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
625 {
626 advance (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000627 v2 = and ();
628 return (value && v2);
Jari Aalto726f6381996-08-26 18:22:31 +0000629 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000630 return (value);
Jari Aalto726f6381996-08-26 18:22:31 +0000631}
632
633/*
634 * or:
635 * and
636 * and '-o' or
637 */
638static int
639or ()
640{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000641 int value, v2;
Jari Aalto726f6381996-08-26 18:22:31 +0000642
643 value = and ();
Jari Aalto726f6381996-08-26 18:22:31 +0000644 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
645 {
646 advance (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000647 v2 = or ();
648 return (value || v2);
Jari Aalto726f6381996-08-26 18:22:31 +0000649 }
650
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000651 return (value);
Jari Aalto726f6381996-08-26 18:22:31 +0000652}
653
654/*
655 * expr:
656 * or
657 */
658static int
659expr ()
660{
661 if (pos >= argc)
662 beyond ();
663
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000664 return (FALSE ^ or ()); /* Same with this. */
Jari Aalto726f6381996-08-26 18:22:31 +0000665}
666
667/* Return TRUE if S is one of the test command's binary operators. */
668static int
669binop (s)
670 char *s;
671{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000672 char *t;
673
674 if (s[0] == '=' && s[1] == '\0')
675 return (1); /* '=' */
Jari Aaltod166f041997-06-05 14:59:13 +0000676 else if ((s[0] == '<' || s[0] == '>') && s[1] == '\0') /* string <, > */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000677 return (1);
Jari Aaltod166f041997-06-05 14:59:13 +0000678 else if ((s[0] == '=' || s[0] == '!') && s[1] == '=' && s[2] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000679 return (1); /* `==' and `!=' */
680#if defined (PATTERN_MATCHING)
681 else if (s[2] == '\0' && s[1] == '~' && (s[0] == '=' || s[0] == '!'))
682 return (1);
683#endif
684 else if (s[0] != '-' || s[2] == '\0' || s[3] != '\0')
685 return (0);
686 else
687 {
688 t = s + 1;
689 if (t[1] == 't')
690 switch (t[0])
691 {
692 case 'n': /* -nt */
693 case 'o': /* -ot */
694 case 'l': /* -lt */
695 case 'g': /* -gt */
696 return (1);
697 default:
698 return (0);
699 }
700 else if (t[0] == 'e')
701 switch (t[1])
702 {
703 case 'q': /* -eq */
704 case 'f': /* -ef */
705 return (1);
706 default:
707 return (0);
708 }
709 else if (t[1] == 'e')
710 switch (t[0])
711 {
712 case 'n': /* -ne */
713 case 'l': /* -le */
714 case 'g': /* -ge */
715 return (1);
716 default:
717 return (0);
718 }
719 else
720 return (0);
721 }
Jari Aalto726f6381996-08-26 18:22:31 +0000722}
723
724/* Return non-zero if OP is one of the test command's unary operators. */
725static int
726unop (op)
727 int op;
728{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000729 switch (op)
730 {
731 case 'a': case 'b': case 'c': case 'd': case 'e':
732 case 'f': case 'g': case 'h': case 'k': case 'n':
733 case 'p': case 'r': case 's': case 't': case 'u':
734 case 'w': case 'x': case 'z':
735 case 'G': case 'L': case 'O': case 'S':
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000736 case 'o':
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000737 return (1);
738 }
739 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000740}
741
742static int
743two_arguments ()
744{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000745 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
746 return (argv[pos + 1][0] == '\0');
747 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000748 {
749 if (unop (argv[pos][1]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000750 return (unary_operator ());
Jari Aalto726f6381996-08-26 18:22:31 +0000751 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000752 test_syntax_error ("%s: unary operator expected", argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000753 }
754 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000755 test_syntax_error ("%s: unary operator expected", argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000756
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000757 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000758}
759
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000760#define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
761
762#define ONE_ARG_TEST(s) ((s)[0] != '\0')
763
Jari Aalto726f6381996-08-26 18:22:31 +0000764static int
765three_arguments ()
766{
767 int value;
768
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000769 if (binop (argv[pos+1]))
Jari Aalto726f6381996-08-26 18:22:31 +0000770 {
771 value = binary_operator ();
772 pos = argc;
773 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000774 else if (ANDOR (argv[pos+1]))
775 {
776 if (argv[pos+1][1] == 'a')
777 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
778 else
779 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
780 pos = argc;
781 }
782 else if (argv[pos][0] == '!' && !argv[pos][1])
783 {
784 advance (1);
785 value = !two_arguments ();
786 }
787 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
788 {
789 value = ONE_ARG_TEST(argv[pos+1]);
790 pos = argc;
791 }
Jari Aalto726f6381996-08-26 18:22:31 +0000792 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000793 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
794
Jari Aalto726f6381996-08-26 18:22:31 +0000795 return (value);
796}
797
798/* This is an implementation of a Posix.2 proposal by David Korn. */
799static int
800posixtest ()
801{
802 int value;
803
804 switch (argc - 1) /* one extra passed in */
805 {
806 case 0:
807 value = FALSE;
808 pos = argc;
809 break;
810
811 case 1:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000812 value = ONE_ARG_TEST(argv[1]);
Jari Aalto726f6381996-08-26 18:22:31 +0000813 pos = argc;
814 break;
815
816 case 2:
817 value = two_arguments ();
818 pos = argc;
819 break;
820
821 case 3:
822 value = three_arguments ();
823 break;
824
825 case 4:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000826 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000827 {
828 advance (1);
829 value = !three_arguments ();
830 break;
831 }
832 /* FALLTHROUGH */
Jari Aalto726f6381996-08-26 18:22:31 +0000833 default:
834 value = expr ();
835 }
836
837 return (value);
838}
839
840/*
841 * [:
842 * '[' expr ']'
843 * test:
844 * test expr
845 */
846int
Jari Aaltod166f041997-06-05 14:59:13 +0000847test_command (margc, margv)
Jari Aalto726f6381996-08-26 18:22:31 +0000848 int margc;
849 char **margv;
850{
851 int value;
852
Jari Aalto726f6381996-08-26 18:22:31 +0000853 int code;
854
855 code = setjmp (test_exit_buf);
856
857 if (code)
858 return (test_error_return);
Jari Aalto726f6381996-08-26 18:22:31 +0000859
860 argv = margv;
861
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000862 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000863 {
864 --margc;
865
866 if (margc < 2)
867 test_exit (SHELL_BOOLEAN (FALSE));
868
869 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000870 test_syntax_error ("missing `]'", (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000871 }
872
873 argc = margc;
874 pos = 1;
875
876 if (pos >= argc)
877 test_exit (SHELL_BOOLEAN (FALSE));
878
879 noeval = 0;
880 value = posixtest ();
881
882 if (pos != argc)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000883 test_syntax_error ("too many arguments", (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000884
885 test_exit (SHELL_BOOLEAN (value));
886}