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, 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 if (interactive_shell == 0)
142 fprintf (stderr, "%s: ", get_name_for_error ());
143 fprintf (stderr, "%s: ", argv[0]);
144 fprintf (stderr, format, arg);
145 fprintf (stderr, "\n");
147 test_exit (TEST_ERREXIT_STATUS);
151 * beyond - call when we're beyond the end of the argument list (an
157 test_syntax_error ("argument expected", (char *)NULL);
160 /* Syntax error for when an integer argument was expected, but
161 something else was found. */
163 integer_expected_error (pch)
166 test_syntax_error ("%s: integer expression expected", pch);
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. */
172 test_stat (path, finfo)
181 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
183 #if !defined (HAVE_DEV_FD)
187 if (legal_number (path + 8, &fd) && fd == (int)fd)
189 r = fstat ((int)fd, finfo);
190 if (r == 0 || errno != EBADF)
196 /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
197 trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
198 On most systems, with the notable exception of linux, this is
199 effectively a no-op. */
201 strcpy (pbuf, DEV_FD_PREFIX);
202 strcat (pbuf, path + 8);
203 return (stat (pbuf, finfo));
204 #endif /* !HAVE_DEV_FD */
206 #if !defined (HAVE_DEV_STDIN)
207 else if (STREQN (path, "/dev/std", 8))
209 if (STREQ (path+8, "in"))
210 return (fstat (0, finfo));
211 else if (STREQ (path+8, "out"))
212 return (fstat (1, finfo));
213 else if (STREQ (path+8, "err"))
214 return (fstat (2, finfo));
216 return (stat (path, finfo));
218 #endif /* !HAVE_DEV_STDIN */
219 return (stat (path, finfo));
222 /* Do the same thing access(2) does, but use the effective uid and gid,
223 and don't make the mistake of telling root that any file is
226 test_eaccess (path, mode)
232 if (test_stat (path, &st) < 0)
235 if (current_user.euid == 0)
237 /* Root can read or write any file. */
241 /* Root can execute any file that has any one of the execute
243 if (st.st_mode & S_IXUGO)
247 if (st.st_uid == current_user.euid) /* owner */
249 else if (group_member (st.st_gid))
252 if (st.st_mode & mode)
259 /* Increment our position in the argument list. Check that we're not
260 past the end of the argument list. This check is supressed if the
261 argument is FALSE. Made a macro for efficiency. */
262 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
263 #define unary_advance() do { advance (1); ++pos; } while (0)
275 return (FALSE ^ or ()); /* Same with this. */
289 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
293 return (value || v2);
310 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
314 return (value && v2);
320 * term - parse a term and return 1 or 0 depending on whether the term
321 * evaluates to true or false, respectively.
324 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
325 * '-'('G'|'L'|'O'|'S'|'N') filename
327 * '-'('z'|'n') string
330 * string ('!='|'='|'==') string
331 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
332 * file '-'(nt|ot|ef) file
335 * positive and negative integers
345 /* Deal with leading `not's. */
346 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
349 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
355 return (value ? !term() : term());
358 /* A paren-bracketed argument. */
359 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
364 test_syntax_error ("`)' expected", (char *)NULL);
365 else if (argv[pos][0] != ')' || argv[pos][1])
366 test_syntax_error ("`)' expected, found %s", argv[pos]);
371 /* are there enough arguments left that this could be dyadic? */
372 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
373 value = binary_operator ();
375 /* Might be a switch type argument */
376 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
378 if (test_unop (argv[pos]))
379 value = unary_operator ();
381 test_syntax_error ("%s: unary operator expected", argv[pos]);
385 value = argv[pos][0] != '\0';
397 struct stat st1, st2;
399 if (test_stat (s, &st1) < 0)
405 if (test_stat (t, &st2) < 0)
414 case OT: return (st1.st_mtime < st2.st_mtime);
415 case NT: return (st1.st_mtime > st2.st_mtime);
416 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
422 arithcomp (s, t, op, flags)
429 if (flags & TEST_ARITHEXP)
431 l = evalexp (s, &expok);
433 return (FALSE); /* should probably longjmp here */
434 r = evalexp (t, &expok);
436 return (FALSE); /* ditto */
440 if (legal_number (s, &l) == 0)
441 integer_expected_error (s);
442 if (legal_number (t, &r) == 0)
443 integer_expected_error (t);
448 case EQ: return (l == r);
449 case NE: return (l != r);
450 case LT: return (l < r);
451 case GT: return (l > r);
452 case LE: return (l <= r);
453 case GE: return (l >= r);
460 patcomp (string, pat, op)
466 m = strmatch (pat, string, FNMATCH_EXTFLAG);
467 return ((op == EQ) ? (m == 0) : (m != 0));
471 binary_test (op, arg1, arg2, flags)
472 char *op, *arg1, *arg2;
477 patmatch = (flags & TEST_PATMATCH);
479 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
480 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
482 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
483 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
485 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
486 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
488 else if (op[2] == 't')
492 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
493 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
494 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
495 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
498 else if (op[1] == 'e')
502 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
503 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
506 else if (op[2] == 'e')
510 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
511 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
512 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
516 return (FALSE); /* should never get here */
527 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
528 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
529 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
531 value = binary_test (w, argv[pos], argv[pos + 2], 0);
536 #if defined (PATTERN_MATCHING)
537 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
539 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
545 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
547 test_syntax_error ("%s: binary operator expected", w);
552 value = binary_test (w, argv[pos], argv[pos + 2], 0);
564 if (test_unop (op) == 0)
567 /* the only tricky case is `-t', which may or may not take an argument. */
573 if (legal_number (argv[pos], &r))
576 return (unary_test (op, argv[pos - 1]));
582 return (unary_test (op, "1"));
585 /* All of the unary operators take an argument, so we first call
586 unary_advance (), which checks to make sure that there is an
587 argument, and then advances pos right past it. This means that
588 pos - 1 is the location of the argument. */
590 return (unary_test (op, argv[pos - 1]));
598 struct stat stat_buf;
602 case 'a': /* file exists in the file system? */
604 return (test_stat (arg, &stat_buf) == 0);
606 case 'r': /* file is readable? */
607 return (EACCESS (arg, R_OK) == 0);
609 case 'w': /* File is writeable? */
610 return (EACCESS (arg, W_OK) == 0);
612 case 'x': /* File is executable? */
613 return (EACCESS (arg, X_OK) == 0);
615 case 'O': /* File is owned by you? */
616 return (test_stat (arg, &stat_buf) == 0 &&
617 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
619 case 'G': /* File is owned by your group? */
620 return (test_stat (arg, &stat_buf) == 0 &&
621 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
624 return (test_stat (arg, &stat_buf) == 0 &&
625 stat_buf.st_atime <= stat_buf.st_mtime);
627 case 'f': /* File is a file? */
628 if (test_stat (arg, &stat_buf) < 0)
631 /* -f is true if the given file exists and is a regular file. */
633 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
635 return (S_ISREG (stat_buf.st_mode));
638 case 'd': /* File is a directory? */
639 return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
641 case 's': /* File has something in it? */
642 return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
644 case 'S': /* File is a socket? */
645 #if !defined (S_ISSOCK)
648 return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
649 #endif /* S_ISSOCK */
651 case 'c': /* File is character special? */
652 return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
654 case 'b': /* File is block special? */
655 return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
657 case 'p': /* File is a named pipe? */
661 return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
662 #endif /* S_ISFIFO */
664 case 'L': /* Same as -h */
665 case 'h': /* File is a symbolic link? */
666 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
669 return ((arg[0] != '\0') &&
670 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
671 #endif /* S_IFLNK && HAVE_LSTAT */
673 case 'u': /* File is setuid? */
674 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
676 case 'g': /* File is setgid? */
677 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
679 case 'k': /* File has sticky bit set? */
680 #if !defined (S_ISVTX)
681 /* This is not Posix, and is not defined on some Posix systems. */
684 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
687 case 't': /* File fd is a terminal? */
688 if (legal_number (arg, &r) == 0)
690 return ((r == (int)r) && isatty ((int)r));
692 case 'n': /* True if arg has some length. */
693 return (arg[0] != '\0');
695 case 'z': /* True if arg has no length. */
696 return (arg[0] == '\0');
698 case 'o': /* True if option `arg' is set. */
699 return (minus_o_option_value (arg) == 1);
702 /* We can't actually get here, but this shuts up gcc. */
706 /* Return TRUE if OP is one of the test command's binary operators. */
711 if (op[0] == '=' && op[1] == '\0')
712 return (1); /* '=' */
713 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
715 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
716 return (1); /* `==' and `!=' */
717 #if defined (PATTERN_MATCHING)
718 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
721 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
736 else if (op[1] == 'e')
745 else if (op[2] == 'e')
760 /* Return non-zero if OP is one of the test command's unary operators. */
770 case 'a': case 'b': case 'c': case 'd': case 'e':
771 case 'f': case 'g': case 'h': case 'k': case 'n':
772 case 'o': case 'p': case 'r': case 's': case 't':
773 case 'u': case 'w': case 'x': case 'z':
774 case 'G': case 'L': case 'O': case 'S': case 'N':
784 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
785 return (argv[pos + 1][0] == '\0');
786 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
788 if (test_unop (argv[pos]))
789 return (unary_operator ());
791 test_syntax_error ("%s: unary operator expected", argv[pos]);
794 test_syntax_error ("%s: unary operator expected", argv[pos]);
799 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
801 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
802 POSIX requires that `-t' be given an argument. */
803 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
810 if (test_binop (argv[pos+1]))
812 value = binary_operator ();
815 else if (ANDOR (argv[pos+1]))
817 if (argv[pos+1][1] == 'a')
818 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
820 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
823 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
826 value = !two_arguments ();
828 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
830 value = ONE_ARG_TEST(argv[pos+1]);
834 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
839 /* This is an implementation of a Posix.2 proposal by David Korn. */
845 switch (argc - 1) /* one extra passed in */
853 value = ONE_ARG_TEST(argv[1]);
858 value = two_arguments ();
863 value = three_arguments ();
867 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
870 value = !three_arguments ();
888 test_command (margc, margv)
897 code = setjmp (test_exit_buf);
900 return (test_error_return);
904 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
908 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
909 test_syntax_error ("missing `]'", (char *)NULL);
912 test_exit (SHELL_BOOLEAN (FALSE));
919 test_exit (SHELL_BOOLEAN (FALSE));
922 value = posixtest ();
925 test_syntax_error ("too many arguments", (char *)NULL);
927 test_exit (SHELL_BOOLEAN (value));