1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
25 /* #define PATTERN_MATCHING */
27 #if defined (HAVE_CONFIG_H)
33 #include "bashtypes.h"
35 #if !defined (HAVE_LIMITS_H)
36 # include <sys/param.h>
39 #if defined (HAVE_UNISTD_H)
48 #if !defined (_POSIX_VERSION)
49 # include <sys/file.h>
50 #endif /* !_POSIX_VERSION */
51 #include "posixstat.h"
57 #include "builtins/common.h"
59 #include <glob/strmatch.h>
62 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
66 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
87 /* The following few defines control the truth and false output of each stage.
88 TRUE and FALSE are what we use to compute the final output value.
89 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
90 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
93 #define SHELL_BOOLEAN(value) (!(value))
95 #define TEST_ERREXIT_STATUS 2
97 static procenv_t test_exit_buf;
98 static int test_error_return;
99 #define test_exit(val) \
100 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
102 /* We have to use access(2) for machines running AFS, because it's
103 not a Unix file system. This may produce incorrect answers for
104 non-AFS files. I hate AFS. */
106 # define EACCESS(path, mode) access(path, mode)
108 # define EACCESS(path, mode) test_eaccess(path, mode)
111 static int pos; /* The offset of the current argument in ARGV. */
112 static int argc; /* The number of arguments present in ARGV. */
113 static char **argv; /* The argument list. */
116 static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
117 static void beyond __P((void)) __attribute__((__noreturn__));
118 static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
120 static int test_stat __P((char *, struct stat *));
122 static int unary_operator __P((void));
123 static int binary_operator __P((void));
124 static int two_arguments __P((void));
125 static int three_arguments __P((void));
126 static int posixtest __P((void));
128 static int expr __P((void));
129 static int term __P((void));
130 static int and __P((void));
131 static int or __P((void));
133 static int filecomp __P((char *, char *, int));
134 static int arithcomp __P((char *, char *, int, int));
135 static int patcomp __P((char *, char *, int));
138 test_syntax_error (format, arg)
141 builtin_error (format, arg);
142 test_exit (TEST_ERREXIT_STATUS);
146 * beyond - call when we're beyond the end of the argument list (an
152 test_syntax_error ("argument expected", (char *)NULL);
155 /* Syntax error for when an integer argument was expected, but
156 something else was found. */
158 integer_expected_error (pch)
161 test_syntax_error ("%s: integer expression expected", pch);
164 /* A wrapper for stat () which disallows pathnames that are empty strings
165 and handles /dev/fd emulation on systems that don't have it. */
167 test_stat (path, finfo)
176 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
178 #if !defined (HAVE_DEV_FD)
182 if (legal_number (path + 8, &fd) && fd == (int)fd)
184 r = fstat ((int)fd, finfo);
185 if (r == 0 || errno != EBADF)
191 /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
192 trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
193 On most systems, with the notable exception of linux, this is
194 effectively a no-op. */
196 strcpy (pbuf, DEV_FD_PREFIX);
197 strcat (pbuf, path + 8);
198 return (stat (pbuf, finfo));
199 #endif /* !HAVE_DEV_FD */
201 #if !defined (HAVE_DEV_STDIN)
202 else if (STREQN (path, "/dev/std", 8))
204 if (STREQ (path+8, "in"))
205 return (fstat (0, finfo));
206 else if (STREQ (path+8, "out"))
207 return (fstat (1, finfo));
208 else if (STREQ (path+8, "err"))
209 return (fstat (2, finfo));
211 return (stat (path, finfo));
213 #endif /* !HAVE_DEV_STDIN */
214 return (stat (path, finfo));
217 /* Do the same thing access(2) does, but use the effective uid and gid,
218 and don't make the mistake of telling root that any file is
221 test_eaccess (path, mode)
227 if (test_stat (path, &st) < 0)
230 if (current_user.euid == 0)
232 /* Root can read or write any file. */
236 /* Root can execute any file that has any one of the execute
238 if (st.st_mode & S_IXUGO)
242 if (st.st_uid == current_user.euid) /* owner */
244 else if (group_member (st.st_gid))
247 if (st.st_mode & mode)
254 /* Increment our position in the argument list. Check that we're not
255 past the end of the argument list. This check is supressed if the
256 argument is FALSE. Made a macro for efficiency. */
257 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
258 #define unary_advance() do { advance (1); ++pos; } while (0)
270 return (FALSE ^ or ()); /* Same with this. */
284 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
288 return (value || v2);
305 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
309 return (value && v2);
315 * term - parse a term and return 1 or 0 depending on whether the term
316 * evaluates to true or false, respectively.
319 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
320 * '-'('G'|'L'|'O'|'S'|'N') filename
322 * '-'('z'|'n') string
325 * string ('!='|'='|'==') string
326 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
327 * file '-'(nt|ot|ef) file
330 * positive and negative integers
340 /* Deal with leading `not's. */
341 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
344 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
350 return (value ? !term() : term());
353 /* A paren-bracketed argument. */
354 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
359 test_syntax_error ("`)' expected", (char *)NULL);
360 else if (argv[pos][0] != ')' || argv[pos][1])
361 test_syntax_error ("`)' expected, found %s", argv[pos]);
366 /* are there enough arguments left that this could be dyadic? */
367 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
368 value = binary_operator ();
370 /* Might be a switch type argument */
371 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
373 if (test_unop (argv[pos]))
374 value = unary_operator ();
376 test_syntax_error ("%s: unary operator expected", argv[pos]);
380 value = argv[pos][0] != '\0';
392 struct stat st1, st2;
395 if ((r1 = test_stat (s, &st1)) < 0)
400 if ((r2 = test_stat (t, &st2)) < 0)
408 case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
409 case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
410 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
416 arithcomp (s, t, op, flags)
423 if (flags & TEST_ARITHEXP)
425 l = evalexp (s, &expok);
427 return (FALSE); /* should probably longjmp here */
428 r = evalexp (t, &expok);
430 return (FALSE); /* ditto */
434 if (legal_number (s, &l) == 0)
435 integer_expected_error (s);
436 if (legal_number (t, &r) == 0)
437 integer_expected_error (t);
442 case EQ: return (l == r);
443 case NE: return (l != r);
444 case LT: return (l < r);
445 case GT: return (l > r);
446 case LE: return (l <= r);
447 case GE: return (l >= r);
454 patcomp (string, pat, op)
460 m = strmatch (pat, string, FNMATCH_EXTFLAG);
461 return ((op == EQ) ? (m == 0) : (m != 0));
465 binary_test (op, arg1, arg2, flags)
466 char *op, *arg1, *arg2;
471 patmatch = (flags & TEST_PATMATCH);
473 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
474 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
476 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
477 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
479 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
480 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
482 else if (op[2] == 't')
486 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
487 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
488 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
489 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
492 else if (op[1] == 'e')
496 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
497 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
500 else if (op[2] == 'e')
504 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
505 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
506 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
510 return (FALSE); /* should never get here */
521 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
522 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
523 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
525 value = binary_test (w, argv[pos], argv[pos + 2], 0);
530 #if defined (PATTERN_MATCHING)
531 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
533 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
539 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
541 test_syntax_error ("%s: binary operator expected", w);
546 value = binary_test (w, argv[pos], argv[pos + 2], 0);
558 if (test_unop (op) == 0)
561 /* the only tricky case is `-t', which may or may not take an argument. */
567 if (legal_number (argv[pos], &r))
570 return (unary_test (op, argv[pos - 1]));
576 return (unary_test (op, "1"));
579 /* All of the unary operators take an argument, so we first call
580 unary_advance (), which checks to make sure that there is an
581 argument, and then advances pos right past it. This means that
582 pos - 1 is the location of the argument. */
584 return (unary_test (op, argv[pos - 1]));
592 struct stat stat_buf;
596 case 'a': /* file exists in the file system? */
598 return (test_stat (arg, &stat_buf) == 0);
600 case 'r': /* file is readable? */
601 return (EACCESS (arg, R_OK) == 0);
603 case 'w': /* File is writeable? */
604 return (EACCESS (arg, W_OK) == 0);
606 case 'x': /* File is executable? */
607 return (EACCESS (arg, X_OK) == 0);
609 case 'O': /* File is owned by you? */
610 return (test_stat (arg, &stat_buf) == 0 &&
611 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
613 case 'G': /* File is owned by your group? */
614 return (test_stat (arg, &stat_buf) == 0 &&
615 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
618 return (test_stat (arg, &stat_buf) == 0 &&
619 stat_buf.st_atime <= stat_buf.st_mtime);
621 case 'f': /* File is a file? */
622 if (test_stat (arg, &stat_buf) < 0)
625 /* -f is true if the given file exists and is a regular file. */
627 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
629 return (S_ISREG (stat_buf.st_mode));
632 case 'd': /* File is a directory? */
633 return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
635 case 's': /* File has something in it? */
636 return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
638 case 'S': /* File is a socket? */
639 #if !defined (S_ISSOCK)
642 return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
643 #endif /* S_ISSOCK */
645 case 'c': /* File is character special? */
646 return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
648 case 'b': /* File is block special? */
649 return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
651 case 'p': /* File is a named pipe? */
655 return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
656 #endif /* S_ISFIFO */
658 case 'L': /* Same as -h */
659 case 'h': /* File is a symbolic link? */
660 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
663 return ((arg[0] != '\0') &&
664 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
665 #endif /* S_IFLNK && HAVE_LSTAT */
667 case 'u': /* File is setuid? */
668 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
670 case 'g': /* File is setgid? */
671 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
673 case 'k': /* File has sticky bit set? */
674 #if !defined (S_ISVTX)
675 /* This is not Posix, and is not defined on some Posix systems. */
678 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
681 case 't': /* File fd is a terminal? */
682 if (legal_number (arg, &r) == 0)
684 return ((r == (int)r) && isatty ((int)r));
686 case 'n': /* True if arg has some length. */
687 return (arg[0] != '\0');
689 case 'z': /* True if arg has no length. */
690 return (arg[0] == '\0');
692 case 'o': /* True if option `arg' is set. */
693 return (minus_o_option_value (arg) == 1);
696 /* We can't actually get here, but this shuts up gcc. */
700 /* Return TRUE if OP is one of the test command's binary operators. */
705 if (op[0] == '=' && op[1] == '\0')
706 return (1); /* '=' */
707 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
709 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
710 return (1); /* `==' and `!=' */
711 #if defined (PATTERN_MATCHING)
712 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
715 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
730 else if (op[1] == 'e')
739 else if (op[2] == 'e')
754 /* Return non-zero if OP is one of the test command's unary operators. */
764 case 'a': case 'b': case 'c': case 'd': case 'e':
765 case 'f': case 'g': case 'h': case 'k': case 'n':
766 case 'o': case 'p': case 'r': case 's': case 't':
767 case 'u': case 'w': case 'x': case 'z':
768 case 'G': case 'L': case 'O': case 'S': case 'N':
778 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
779 return (argv[pos + 1][0] == '\0');
780 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
782 if (test_unop (argv[pos]))
783 return (unary_operator ());
785 test_syntax_error ("%s: unary operator expected", argv[pos]);
788 test_syntax_error ("%s: unary operator expected", argv[pos]);
793 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
795 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
796 POSIX requires that `-t' be given an argument. */
797 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
804 if (test_binop (argv[pos+1]))
806 value = binary_operator ();
809 else if (ANDOR (argv[pos+1]))
811 if (argv[pos+1][1] == 'a')
812 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
814 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
817 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
820 value = !two_arguments ();
822 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
824 value = ONE_ARG_TEST(argv[pos+1]);
828 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
833 /* This is an implementation of a Posix.2 proposal by David Korn. */
839 switch (argc - 1) /* one extra passed in */
847 value = ONE_ARG_TEST(argv[1]);
852 value = two_arguments ();
857 value = three_arguments ();
861 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
864 value = !three_arguments ();
882 test_command (margc, margv)
891 code = setjmp (test_exit_buf);
894 return (test_error_return);
898 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
902 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
903 test_syntax_error ("missing `]'", (char *)NULL);
906 test_exit (SHELL_BOOLEAN (FALSE));
913 test_exit (SHELL_BOOLEAN (FALSE));
916 value = posixtest ();
919 test_syntax_error ("too many arguments", (char *)NULL);
921 test_exit (SHELL_BOOLEAN (value));