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)
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 #define TEST_ERREXIT_STATUS 2
103 static procenv_t test_exit_buf;
104 static int test_error_return;
105 #define test_exit(val) \
106 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
108 /* We have to use access(2) for machines running AFS, because it's
109 not a Unix file system. This may produce incorrect answers for
110 non-AFS files. I hate AFS. */
112 # define EACCESS(path, mode) access(path, mode)
114 # define EACCESS(path, mode) test_eaccess(path, mode)
117 static int pos; /* The offset of the current argument in ARGV. */
118 static int argc; /* The number of arguments present in ARGV. */
119 static char **argv; /* The argument list. */
122 static int unary_operator ();
123 static int binary_operator ();
124 static int two_arguments ();
125 static int three_arguments ();
126 static int posixtest ();
133 static void beyond ();
136 test_syntax_error (format, arg)
139 extern int interactive_shell;
140 extern char *get_name_for_error ();
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)
185 if (legal_number (path + 8, &fd))
186 return (fstat ((int)fd, finfo));
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 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
290 return (value || v2);
307 while (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')
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;
396 if (test_stat (s, &st1) < 0 || test_stat (t, &st2) < 0)
400 case OT: return (st1.st_mtime < st2.st_mtime);
401 case NT: return (st1.st_mtime > st2.st_mtime);
402 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
408 arithcomp (s, t, op, flags)
415 if (flags & TEST_ARITHEXP)
417 l = evalexp (s, &expok);
419 return (FALSE); /* should probably longjmp here */
420 r = evalexp (t, &expok);
422 return (FALSE); /* ditto */
426 if (legal_number (s, &l) == 0)
427 integer_expected_error (s);
428 if (legal_number (t, &r) == 0)
429 integer_expected_error (t);
434 case EQ: return (l == r);
435 case NE: return (l != r);
436 case LT: return (l < r);
437 case GT: return (l > r);
438 case LE: return (l <= r);
439 case GE: return (l >= r);
446 patcomp (string, pat, op)
452 m = fnmatch (pat, string, FNMATCH_EXTFLAG);
453 return ((op == EQ) ? (m == 0) : (m != 0));
457 binary_test (op, arg1, arg2, flags)
458 char *op, *arg1, *arg2;
463 patmatch = (flags & TEST_PATMATCH);
465 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
466 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
468 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
469 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
471 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
472 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
474 else if (op[2] == 't')
478 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
479 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
480 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
481 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
484 else if (op[1] == 'e')
488 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
489 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
492 else if (op[2] == 'e')
496 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
497 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
498 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
502 return (FALSE); /* should never get here */
513 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
514 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
515 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
517 value = binary_test (w, argv[pos], argv[pos + 2], 0);
522 #if defined (PATTERN_MATCHING)
523 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
525 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
531 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
533 test_syntax_error ("%s: binary operator expected", w);
538 value = binary_test (w, argv[pos], argv[pos + 2], 0);
550 if (test_unop (op) == 0)
553 /* the only tricky case is `-t', which may or may not take an argument. */
557 if (pos < argc && legal_number (argv[pos], &r))
560 return (unary_test (op, argv[pos - 1]));
563 return (unary_test (op, "1"));
566 /* All of the unary operators take an argument, so we first call
567 unary_advance (), which checks to make sure that there is an
568 argument, and then advances pos right past it. This means that
569 pos - 1 is the location of the argument. */
571 return (unary_test (op, argv[pos - 1]));
579 struct stat stat_buf;
583 case 'a': /* file exists in the file system? */
585 return (test_stat (arg, &stat_buf) == 0);
587 case 'r': /* file is readable? */
588 return (EACCESS (arg, R_OK) == 0);
590 case 'w': /* File is writeable? */
591 return (EACCESS (arg, W_OK) == 0);
593 case 'x': /* File is executable? */
594 return (EACCESS (arg, X_OK) == 0);
596 case 'O': /* File is owned by you? */
597 return (test_stat (arg, &stat_buf) == 0 &&
598 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
600 case 'G': /* File is owned by your group? */
601 return (test_stat (arg, &stat_buf) == 0 &&
602 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
605 return (test_stat (arg, &stat_buf) == 0 &&
606 stat_buf.st_atime <= stat_buf.st_mtime);
608 case 'f': /* File is a file? */
609 if (test_stat (arg, &stat_buf) < 0)
612 /* -f is true if the given file exists and is a regular file. */
614 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
616 return (S_ISREG (stat_buf.st_mode));
619 case 'd': /* File is a directory? */
620 return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
622 case 's': /* File has something in it? */
623 return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
625 case 'S': /* File is a socket? */
626 #if !defined (S_ISSOCK)
629 return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
630 #endif /* S_ISSOCK */
632 case 'c': /* File is character special? */
633 return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
635 case 'b': /* File is block special? */
636 return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
638 case 'p': /* File is a named pipe? */
642 return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
643 #endif /* S_ISFIFO */
645 case 'L': /* Same as -h */
646 case 'h': /* File is a symbolic link? */
647 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
650 return ((arg[0] != '\0') &&
651 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
652 #endif /* S_IFLNK && HAVE_LSTAT */
654 case 'u': /* File is setuid? */
655 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
657 case 'g': /* File is setgid? */
658 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
660 case 'k': /* File has sticky bit set? */
661 #if !defined (S_ISVTX)
662 /* This is not Posix, and is not defined on some Posix systems. */
665 return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
668 case 't': /* File fd is a terminal? */
669 if (legal_number (arg, &r) == 0)
671 return (isatty ((int)r));
673 case 'n': /* True if arg has some length. */
674 return (arg[0] != '\0');
676 case 'z': /* True if arg has no length. */
677 return (arg[0] == '\0');
679 case 'o': /* True if option `arg' is set. */
680 return (minus_o_option_value (arg) == 1);
684 /* Return TRUE if OP is one of the test command's binary operators. */
689 if (op[0] == '=' && op[1] == '\0')
690 return (1); /* '=' */
691 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
693 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
694 return (1); /* `==' and `!=' */
695 #if defined (PATTERN_MATCHING)
696 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
699 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
714 else if (op[1] == 'e')
723 else if (op[2] == 'e')
738 /* Return non-zero if OP is one of the test command's unary operators. */
748 case 'a': case 'b': case 'c': case 'd': case 'e':
749 case 'f': case 'g': case 'h': case 'k': case 'n':
750 case 'o': case 'p': case 'r': case 's': case 't':
751 case 'u': case 'w': case 'x': case 'z':
752 case 'G': case 'L': case 'O': case 'S': case 'N':
762 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
763 return (argv[pos + 1][0] == '\0');
764 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
766 if (test_unop (argv[pos]))
767 return (unary_operator ());
769 test_syntax_error ("%s: unary operator expected", argv[pos]);
772 test_syntax_error ("%s: unary operator expected", argv[pos]);
777 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
779 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
786 if (test_binop (argv[pos+1]))
788 value = binary_operator ();
791 else if (ANDOR (argv[pos+1]))
793 if (argv[pos+1][1] == 'a')
794 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
796 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
799 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
802 value = !two_arguments ();
804 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
806 value = ONE_ARG_TEST(argv[pos+1]);
810 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
815 /* This is an implementation of a Posix.2 proposal by David Korn. */
821 switch (argc - 1) /* one extra passed in */
829 value = ONE_ARG_TEST(argv[1]);
834 value = two_arguments ();
839 value = three_arguments ();
843 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
846 value = !three_arguments ();
864 test_command (margc, margv)
872 code = setjmp (test_exit_buf);
875 return (test_error_return);
879 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
884 test_exit (SHELL_BOOLEAN (FALSE));
886 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
887 test_syntax_error ("missing `]'", (char *)NULL);
894 test_exit (SHELL_BOOLEAN (FALSE));
897 value = posixtest ();
900 test_syntax_error ("too many arguments", (char *)NULL);
902 test_exit (SHELL_BOOLEAN (value));