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) && defined (HAVE_SYS_FILE_H)
49 # include <sys/file.h>
50 #endif /* !_POSIX_VERSION */
51 #include "posixstat.h"
59 #include "builtins/common.h"
61 #include <glob/strmatch.h>
64 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
68 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
89 /* 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.
92 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
95 #define SHELL_BOOLEAN(value) (!(value))
97 #define TEST_ERREXIT_STATUS 2
99 static procenv_t test_exit_buf;
100 static int test_error_return;
101 #define test_exit(val) \
102 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
104 /* We have to use access(2) for machines running AFS, because it's
105 not a Unix file system. This may produce incorrect answers for
106 non-AFS files. I hate AFS. */
108 # define EACCESS(path, mode) access(path, mode)
110 # define EACCESS(path, mode) test_eaccess(path, mode)
113 static int pos; /* The offset of the current argument in ARGV. */
114 static int argc; /* The number of arguments present in ARGV. */
115 static char **argv; /* The argument list. */
118 static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
119 static void beyond __P((void)) __attribute__((__noreturn__));
120 static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
122 static int test_stat __P((char *, struct stat *));
124 static int unary_operator __P((void));
125 static int binary_operator __P((void));
126 static int two_arguments __P((void));
127 static int three_arguments __P((void));
128 static int posixtest __P((void));
130 static int expr __P((void));
131 static int term __P((void));
132 static int and __P((void));
133 static int or __P((void));
135 static int filecomp __P((char *, char *, int));
136 static int arithcomp __P((char *, char *, int, int));
137 static int patcomp __P((char *, char *, int));
140 test_syntax_error (format, arg)
143 builtin_error (format, arg);
144 test_exit (TEST_ERREXIT_STATUS);
148 * beyond - call when we're beyond the end of the argument list (an
154 test_syntax_error (_("argument expected"), (char *)NULL);
157 /* Syntax error for when an integer argument was expected, but
158 something else was found. */
160 integer_expected_error (pch)
163 test_syntax_error (_("%s: integer expression expected"), pch);
166 /* A wrapper for stat () which disallows pathnames that are empty strings
167 and handles /dev/fd emulation on systems that don't have it. */
169 test_stat (path, finfo)
178 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
180 #if !defined (HAVE_DEV_FD)
184 if (legal_number (path + 8, &fd) && fd == (int)fd)
186 r = fstat ((int)fd, finfo);
187 if (r == 0 || errno != EBADF)
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. */
198 strcpy (pbuf, DEV_FD_PREFIX);
199 strcat (pbuf, path + 8);
200 return (stat (pbuf, finfo));
201 #endif /* !HAVE_DEV_FD */
203 #if !defined (HAVE_DEV_STDIN)
204 else if (STREQN (path, "/dev/std", 8))
206 if (STREQ (path+8, "in"))
207 return (fstat (0, finfo));
208 else if (STREQ (path+8, "out"))
209 return (fstat (1, finfo));
210 else if (STREQ (path+8, "err"))
211 return (fstat (2, finfo));
213 return (stat (path, finfo));
215 #endif /* !HAVE_DEV_STDIN */
216 return (stat (path, finfo));
219 /* Do the same thing access(2) does, but use the effective uid and gid,
220 and don't make the mistake of telling root that any file is
223 test_eaccess (path, mode)
229 if (test_stat (path, &st) < 0)
232 if (current_user.euid == 0)
234 /* Root can read or write any file. */
238 /* Root can execute any file that has any one of the execute
240 if (st.st_mode & S_IXUGO)
244 if (st.st_uid == current_user.euid) /* owner */
246 else if (group_member (st.st_gid))
249 if (st.st_mode & mode)
256 /* Increment our position in the argument list. Check that we're not
257 past the end of the argument list. This check is supressed if the
258 argument is FALSE. Made a macro for efficiency. */
259 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
260 #define unary_advance() do { advance (1); ++pos; } while (0)
272 return (FALSE ^ or ()); /* Same with this. */
286 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
290 return (value || v2);
307 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
311 return (value && v2);
317 * term - parse a term and return 1 or 0 depending on whether the term
318 * evaluates to true or false, respectively.
321 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
322 * '-'('G'|'L'|'O'|'S'|'N') filename
324 * '-'('z'|'n') string
327 * string ('!='|'='|'==') string
328 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
329 * file '-'(nt|ot|ef) file
332 * positive and negative integers
342 /* Deal with leading `not's. */
343 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
346 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
352 return (value ? !term() : term());
355 /* A paren-bracketed argument. */
356 if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
360 if (argv[pos] == 0) /* ( */
361 test_syntax_error (_("`)' expected"), (char *)NULL);
362 else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */
363 test_syntax_error (_("`)' expected, found %s"), argv[pos]);
368 /* are there enough arguments left that this could be dyadic? */
369 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
370 value = binary_operator ();
372 /* Might be a switch type argument */
373 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
375 if (test_unop (argv[pos]))
376 value = unary_operator ();
378 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
382 value = argv[pos][0] != '\0';
394 struct stat st1, st2;
397 if ((r1 = test_stat (s, &st1)) < 0)
402 if ((r2 = test_stat (t, &st2)) < 0)
410 case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
411 case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
412 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
418 arithcomp (s, t, op, flags)
425 if (flags & TEST_ARITHEXP)
427 l = evalexp (s, &expok);
429 return (FALSE); /* should probably longjmp here */
430 r = evalexp (t, &expok);
432 return (FALSE); /* ditto */
436 if (legal_number (s, &l) == 0)
437 integer_expected_error (s);
438 if (legal_number (t, &r) == 0)
439 integer_expected_error (t);
444 case EQ: return (l == r);
445 case NE: return (l != r);
446 case LT: return (l < r);
447 case GT: return (l > r);
448 case LE: return (l <= r);
449 case GE: return (l >= r);
456 patcomp (string, pat, op)
462 m = strmatch (pat, string, FNMATCH_EXTFLAG);
463 return ((op == EQ) ? (m == 0) : (m != 0));
467 binary_test (op, arg1, arg2, flags)
468 char *op, *arg1, *arg2;
473 patmatch = (flags & TEST_PATMATCH);
475 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
476 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
478 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
479 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
481 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
482 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
484 else if (op[2] == 't')
488 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
489 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
490 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
491 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
494 else if (op[1] == 'e')
498 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
499 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
502 else if (op[2] == 'e')
506 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
507 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
508 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
512 return (FALSE); /* should never get here */
523 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
524 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
525 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
527 value = binary_test (w, argv[pos], argv[pos + 2], 0);
532 #if defined (PATTERN_MATCHING)
533 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
535 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
541 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
543 test_syntax_error (_("%s: binary operator expected"), w);
548 value = binary_test (w, argv[pos], argv[pos + 2], 0);
560 if (test_unop (op) == 0)
563 /* the only tricky case is `-t', which may or may not take an argument. */
569 if (legal_number (argv[pos], &r))
572 return (unary_test (op, argv[pos - 1]));
578 return (unary_test (op, "1"));
581 /* All of the unary operators take an argument, so we first call
582 unary_advance (), which checks to make sure that there is an
583 argument, and then advances pos right past it. This means that
584 pos - 1 is the location of the argument. */
586 return (unary_test (op, argv[pos - 1]));
594 struct stat stat_buf;
598 case 'a': /* file exists in the file system? */
600 return (test_stat (arg, &stat_buf) == 0);
602 case 'r': /* file is readable? */
603 return (EACCESS (arg, R_OK) == 0);
605 case 'w': /* File is writeable? */
606 return (EACCESS (arg, W_OK) == 0);
608 case 'x': /* File is executable? */
609 return (EACCESS (arg, X_OK) == 0);
611 case 'O': /* File is owned by you? */
612 return (test_stat (arg, &stat_buf) == 0 &&
613 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
615 case 'G': /* File is owned by your group? */
616 return (test_stat (arg, &stat_buf) == 0 &&
617 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
620 return (test_stat (arg, &stat_buf) == 0 &&
621 stat_buf.st_atime <= stat_buf.st_mtime);
623 case 'f': /* File is a file? */
624 if (test_stat (arg, &stat_buf) < 0)
627 /* -f is true if the given file exists and is a regular file. */
629 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
631 return (S_ISREG (stat_buf.st_mode));
634 case 'd': /* File is a directory? */
635 return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
637 case 's': /* File has something in it? */
638 return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
640 case 'S': /* File is a socket? */
641 #if !defined (S_ISSOCK)
644 return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
645 #endif /* S_ISSOCK */
647 case 'c': /* File is character special? */
648 return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
650 case 'b': /* File is block special? */
651 return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
653 case 'p': /* File is a named pipe? */
657 return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
658 #endif /* S_ISFIFO */
660 case 'L': /* Same as -h */
661 case 'h': /* File is a symbolic link? */
662 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
665 return ((arg[0] != '\0') &&
666 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
667 #endif /* S_IFLNK && HAVE_LSTAT */
669 case 'u': /* File is setuid? */
670 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
672 case 'g': /* File is setgid? */
673 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
675 case 'k': /* File has sticky bit set? */
676 #if !defined (S_ISVTX)
677 /* This is not Posix, and is not defined on some Posix systems. */
680 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
683 case 't': /* File fd is a terminal? */
684 if (legal_number (arg, &r) == 0)
686 return ((r == (int)r) && isatty ((int)r));
688 case 'n': /* True if arg has some length. */
689 return (arg[0] != '\0');
691 case 'z': /* True if arg has no length. */
692 return (arg[0] == '\0');
694 case 'o': /* True if option `arg' is set. */
695 return (minus_o_option_value (arg) == 1);
698 /* We can't actually get here, but this shuts up gcc. */
702 /* Return TRUE if OP is one of the test command's binary operators. */
707 if (op[0] == '=' && op[1] == '\0')
708 return (1); /* '=' */
709 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
711 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
712 return (1); /* `==' and `!=' */
713 #if defined (PATTERN_MATCHING)
714 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
717 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
732 else if (op[1] == 'e')
741 else if (op[2] == 'e')
756 /* Return non-zero if OP is one of the test command's unary operators. */
766 case 'a': case 'b': case 'c': case 'd': case 'e':
767 case 'f': case 'g': case 'h': case 'k': case 'n':
768 case 'o': case 'p': case 'r': case 's': case 't':
769 case 'u': case 'w': case 'x': case 'z':
770 case 'G': case 'L': case 'O': case 'S': case 'N':
780 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
781 return (argv[pos + 1][0] == '\0');
782 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
784 if (test_unop (argv[pos]))
785 return (unary_operator ());
787 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
790 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
795 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
797 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
798 POSIX requires that `-t' be given an argument. */
799 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
806 if (test_binop (argv[pos+1]))
808 value = binary_operator ();
811 else if (ANDOR (argv[pos+1]))
813 if (argv[pos+1][1] == 'a')
814 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
816 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
819 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
822 value = !two_arguments ();
824 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
826 value = ONE_ARG_TEST(argv[pos+1]);
830 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
835 /* This is an implementation of a Posix.2 proposal by David Korn. */
841 switch (argc - 1) /* one extra passed in */
849 value = ONE_ARG_TEST(argv[1]);
854 value = two_arguments ();
859 value = three_arguments ();
863 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
866 value = !three_arguments ();
884 test_command (margc, margv)
893 code = setjmp (test_exit_buf);
896 return (test_error_return);
900 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
904 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
905 test_syntax_error (_("missing `]'"), (char *)NULL);
908 test_exit (SHELL_BOOLEAN (FALSE));
915 test_exit (SHELL_BOOLEAN (FALSE));
918 value = posixtest ();
921 test_syntax_error (_("too many arguments"), (char *)NULL);
923 test_exit (SHELL_BOOLEAN (value));