1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987, 1988, 1989, 1990, 1991 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, 675 Mass Ave, Cambridge, MA 02139, 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)
38 # include <sys/param.h>
41 #if defined (HAVE_UNISTD_H)
50 #if !defined (_POSIX_VERSION)
51 # include <sys/file.h>
52 #endif /* !_POSIX_VERSION */
53 #include "posixstat.h"
59 #include "builtins/common.h"
61 #include <glob/fnmatch.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)
72 # define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0)
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.
96 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
99 #define SHELL_BOOLEAN(value) (!(value))
101 static procenv_t test_exit_buf;
102 static int test_error_return;
103 #define test_exit(val) \
104 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
106 /* We have to use access(2) for machines running AFS, because it's
107 not a Unix file system. This may produce incorrect answers for
108 non-AFS files. I hate AFS. */
110 # define EACCESS(path, mode) access(path, mode)
112 # define EACCESS(path, mode) test_eaccess(path, mode)
115 static int pos; /* The offset of the current argument in ARGV. */
116 static int argc; /* The number of arguments present in ARGV. */
117 static char **argv; /* The argument list. */
120 static int unary_operator ();
121 static int binary_operator ();
122 static int two_arguments ();
123 static int three_arguments ();
124 static int posixtest ();
131 static void beyond ();
134 test_syntax_error (format, arg)
137 extern int interactive_shell;
138 extern char *get_name_for_error ();
139 if (interactive_shell == 0)
140 fprintf (stderr, "%s: ", get_name_for_error ());
141 fprintf (stderr, "%s: ", argv[0]);
142 fprintf (stderr, format, arg);
143 fprintf (stderr, "\n");
145 test_exit (SHELL_BOOLEAN (FALSE));
149 * beyond - call when we're beyond the end of the argument list (an
155 test_syntax_error ("argument expected", (char *)NULL);
158 /* Syntax error for when an integer argument was expected, but
159 something else was found. */
161 integer_expected_error (pch)
164 test_syntax_error ("%s: integer expression expected", pch);
167 /* A wrapper for stat () which disallows pathnames that are empty strings
168 and handles /dev/fd emulation on systems that don't have it. */
170 test_stat (path, finfo)
179 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
181 #if !defined (HAVE_DEV_FD)
183 if (legal_number (path + 8, &fd))
184 return (fstat ((int)fd, finfo));
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 return (stat (path, finfo));
204 /* Do the same thing access(2) does, but use the effective uid and gid,
205 and don't make the mistake of telling root that any file is
208 test_eaccess (path, mode)
214 if (test_stat (path, &st) < 0)
217 if (current_user.euid == 0)
219 /* Root can read or write any file. */
223 /* Root can execute any file that has any one of the execute
225 if (st.st_mode & S_IXUGO)
229 if (st.st_uid == current_user.euid) /* owner */
231 else if (group_member (st.st_gid))
234 if (st.st_mode & mode)
241 /* Increment our position in the argument list. Check that we're not
242 past the end of the argument list. This check is supressed if the
243 argument is FALSE. Made a macro for efficiency. */
244 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
245 #define unary_advance() do { advance (1); ++pos; } while (0)
257 return (FALSE ^ or ()); /* Same with this. */
271 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
275 return (value || v2);
292 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
296 return (value && v2);
302 * term - parse a term and return 1 or 0 depending on whether the term
303 * evaluates to true or false, respectively.
306 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
307 * '-'('G'|'L'|'O'|'S'|'N') filename
309 * '-'('z'|'n') string
312 * string ('!='|'='|'==') string
313 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
314 * file '-'(nt|ot|ef) file
317 * positive and negative integers
327 /* Deal with leading `not's. */
328 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
331 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
337 return (value ? !term() : term());
340 /* A paren-bracketed argument. */
341 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
346 test_syntax_error ("`)' expected", (char *)NULL);
347 else if (argv[pos][0] != ')' || argv[pos][1])
348 test_syntax_error ("`)' expected, found %s", argv[pos]);
353 /* are there enough arguments left that this could be dyadic? */
354 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
355 value = binary_operator ();
357 /* Might be a switch type argument */
358 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
360 if (test_unop (argv[pos]))
361 value = unary_operator ();
363 test_syntax_error ("%s: unary operator expected", argv[pos]);
367 value = argv[pos][0] != '\0';
379 struct stat st1, st2;
381 if (test_stat (s, &st1) < 0 || test_stat (t, &st2) < 0)
385 case OT: return (st1.st_mtime < st2.st_mtime);
386 case NT: return (st1.st_mtime > st2.st_mtime);
387 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
393 arithcomp (s, t, op, flags)
400 if (flags & TEST_ARITHEXP)
402 l = evalexp (s, &expok);
404 return (FALSE); /* should probably longjmp here */
405 r = evalexp (t, &expok);
407 return (FALSE); /* ditto */
411 if (legal_number (s, &l) == 0)
412 integer_expected_error (s);
413 if (legal_number (t, &r) == 0)
414 integer_expected_error (t);
419 case EQ: return (l == r);
420 case NE: return (l != r);
421 case LT: return (l < r);
422 case GT: return (l > r);
423 case LE: return (l <= r);
424 case GE: return (l >= r);
431 patcomp (string, pat, op)
437 m = fnmatch (pat, string, FNMATCH_EXTFLAG);
438 return ((op == EQ) ? (m == 0) : (m != 0));
442 binary_test (op, arg1, arg2, flags)
443 char *op, *arg1, *arg2;
448 patmatch = (flags & TEST_PATMATCH);
450 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
451 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
453 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
454 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
456 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
457 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
459 else if (op[2] == 't')
463 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
464 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
465 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
466 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
469 else if (op[1] == 'e')
473 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
474 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
477 else if (op[2] == 'e')
481 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
482 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
483 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
487 return (FALSE); /* should never get here */
498 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
499 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
500 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
502 value = binary_test (w, argv[pos], argv[pos + 2], 0);
507 #if defined (PATTERN_MATCHING)
508 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
510 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
516 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
518 test_syntax_error ("%s: binary operator expected", w);
523 value = binary_test (w, argv[pos], argv[pos + 2], 0);
535 if (test_unop (op) == 0)
538 /* the only tricky case is `-t', which may or may not take an argument. */
542 if (pos < argc && legal_number (argv[pos], &r))
545 return (unary_test (op, argv[pos - 1]));
548 return (unary_test (op, "1"));
551 /* All of the unary operators take an argument, so we first call
552 unary_advance (), which checks to make sure that there is an
553 argument, and then advances pos right past it. This means that
554 pos - 1 is the location of the argument. */
556 return (unary_test (op, argv[pos - 1]));
564 struct stat stat_buf;
568 case 'a': /* file exists in the file system? */
570 return (test_stat (arg, &stat_buf) == 0);
572 case 'r': /* file is readable? */
573 return (EACCESS (arg, R_OK) == 0);
575 case 'w': /* File is writeable? */
576 return (EACCESS (arg, W_OK) == 0);
578 case 'x': /* File is executable? */
579 return (EACCESS (arg, X_OK) == 0);
581 case 'O': /* File is owned by you? */
582 return (test_stat (arg, &stat_buf) == 0 &&
583 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
585 case 'G': /* File is owned by your group? */
586 return (test_stat (arg, &stat_buf) == 0 &&
587 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
590 return (test_stat (arg, &stat_buf) == 0 &&
591 stat_buf.st_atime <= stat_buf.st_mtime);
593 case 'f': /* File is a file? */
594 if (test_stat (arg, &stat_buf) < 0)
597 /* -f is true if the given file exists and is a regular file. */
599 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
601 return (S_ISREG (stat_buf.st_mode));
604 case 'd': /* File is a directory? */
605 return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
607 case 's': /* File has something in it? */
608 return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
610 case 'S': /* File is a socket? */
611 #if !defined (S_ISSOCK)
614 return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
615 #endif /* S_ISSOCK */
617 case 'c': /* File is character special? */
618 return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
620 case 'b': /* File is block special? */
621 return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
623 case 'p': /* File is a named pipe? */
627 return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
628 #endif /* S_ISFIFO */
630 case 'L': /* Same as -h */
631 case 'h': /* File is a symbolic link? */
632 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
635 return ((arg[0] != '\0') &&
636 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
637 #endif /* S_IFLNK && HAVE_LSTAT */
639 case 'u': /* File is setuid? */
640 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
642 case 'g': /* File is setgid? */
643 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
645 case 'k': /* File has sticky bit set? */
646 #if !defined (S_ISVTX)
647 /* This is not Posix, and is not defined on some Posix systems. */
650 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
653 case 't': /* File fd is a terminal? */
654 if (legal_number (arg, &r) == 0)
656 return (isatty ((int)r));
658 case 'n': /* True if arg has some length. */
659 return (arg[0] != '\0');
661 case 'z': /* True if arg has no length. */
662 return (arg[0] == '\0');
664 case 'o': /* True if option `arg' is set. */
665 return (minus_o_option_value (arg) == 1);
669 /* Return TRUE if OP is one of the test command's binary operators. */
674 if (op[0] == '=' && op[1] == '\0')
675 return (1); /* '=' */
676 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
678 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
679 return (1); /* `==' and `!=' */
680 #if defined (PATTERN_MATCHING)
681 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
684 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
699 else if (op[1] == 'e')
708 else if (op[2] == 'e')
723 /* Return non-zero if OP is one of the test command's unary operators. */
733 case 'a': case 'b': case 'c': case 'd': case 'e':
734 case 'f': case 'g': case 'h': case 'k': case 'n':
735 case 'o': case 'p': case 'r': case 's': case 't':
736 case 'u': case 'w': case 'x': case 'z':
737 case 'G': case 'L': case 'O': case 'S': case 'N':
747 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
748 return (argv[pos + 1][0] == '\0');
749 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
751 if (test_unop (argv[pos]))
752 return (unary_operator ());
754 test_syntax_error ("%s: unary operator expected", argv[pos]);
757 test_syntax_error ("%s: unary operator expected", argv[pos]);
762 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
764 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
771 if (test_binop (argv[pos+1]))
773 value = binary_operator ();
776 else if (ANDOR (argv[pos+1]))
778 if (argv[pos+1][1] == 'a')
779 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
781 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
784 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
787 value = !two_arguments ();
789 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
791 value = ONE_ARG_TEST(argv[pos+1]);
795 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
800 /* This is an implementation of a Posix.2 proposal by David Korn. */
806 switch (argc - 1) /* one extra passed in */
814 value = ONE_ARG_TEST(argv[1]);
819 value = two_arguments ();
824 value = three_arguments ();
828 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
831 value = !three_arguments ();
849 test_command (margc, margv)
857 code = setjmp (test_exit_buf);
860 return (test_error_return);
864 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
869 test_exit (SHELL_BOOLEAN (FALSE));
871 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
872 test_syntax_error ("missing `]'", (char *)NULL);
879 test_exit (SHELL_BOOLEAN (FALSE));
882 value = posixtest ();
885 test_syntax_error ("too many arguments", (char *)NULL);
887 test_exit (SHELL_BOOLEAN (value));