Patch-ID: bash40-034
[bash.git] / test.c
blobad634f9ab2200f00e8ec3a027f92229e92db6e2a
1 /* test.c - GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987-2009 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
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
24 binary operators. */
25 /* #define PATTERN_MATCHING */
27 #if defined (HAVE_CONFIG_H)
28 # include <config.h>
29 #endif
31 #include <stdio.h>
33 #include "bashtypes.h"
35 #if !defined (HAVE_LIMITS_H)
36 # include <sys/param.h>
37 #endif
39 #if defined (HAVE_UNISTD_H)
40 # include <unistd.h>
41 #endif
43 #include <errno.h>
44 #if !defined (errno)
45 extern int errno;
46 #endif /* !errno */
48 #if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
49 # include <sys/file.h>
50 #endif /* !_POSIX_VERSION */
51 #include "posixstat.h"
52 #include "filecntl.h"
54 #include "bashintl.h"
56 #include "shell.h"
57 #include "pathexp.h"
58 #include "test.h"
59 #include "builtins/common.h"
61 #include <glob/strmatch.h>
63 #if !defined (STRLEN)
64 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
65 #endif
67 #if !defined (STREQ)
68 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
69 #endif /* !STREQ */
71 #if !defined (R_OK)
72 #define R_OK 4
73 #define W_OK 2
74 #define X_OK 1
75 #define F_OK 0
76 #endif /* R_OK */
78 #define EQ 0
79 #define NE 1
80 #define LT 2
81 #define GT 3
82 #define LE 4
83 #define GE 5
85 #define NT 0
86 #define OT 1
87 #define EF 2
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). */
93 #define TRUE 1
94 #define FALSE 0
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 extern int sh_stat __P((const char *, struct stat *));
106 static int pos; /* The offset of the current argument in ARGV. */
107 static int argc; /* The number of arguments present in ARGV. */
108 static char **argv; /* The argument list. */
109 static int noeval;
111 static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
112 static void beyond __P((void)) __attribute__((__noreturn__));
113 static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
115 static int unary_operator __P((void));
116 static int binary_operator __P((void));
117 static int two_arguments __P((void));
118 static int three_arguments __P((void));
119 static int posixtest __P((void));
121 static int expr __P((void));
122 static int term __P((void));
123 static int and __P((void));
124 static int or __P((void));
126 static int filecomp __P((char *, char *, int));
127 static int arithcomp __P((char *, char *, int, int));
128 static int patcomp __P((char *, char *, int));
130 static void
131 test_syntax_error (format, arg)
132 char *format, *arg;
134 builtin_error (format, arg);
135 test_exit (TEST_ERREXIT_STATUS);
139 * beyond - call when we're beyond the end of the argument list (an
140 * error condition)
142 static void
143 beyond ()
145 test_syntax_error (_("argument expected"), (char *)NULL);
148 /* Syntax error for when an integer argument was expected, but
149 something else was found. */
150 static void
151 integer_expected_error (pch)
152 char *pch;
154 test_syntax_error (_("%s: integer expression expected"), pch);
157 /* Increment our position in the argument list. Check that we're not
158 past the end of the argument list. This check is supressed if the
159 argument is FALSE. Made a macro for efficiency. */
160 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
161 #define unary_advance() do { advance (1); ++pos; } while (0)
164 * expr:
165 * or
167 static int
168 expr ()
170 if (pos >= argc)
171 beyond ();
173 return (FALSE ^ or ()); /* Same with this. */
177 * or:
178 * and
179 * and '-o' or
181 static int
182 or ()
184 int value, v2;
186 value = and ();
187 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
189 advance (0);
190 v2 = or ();
191 return (value || v2);
194 return (value);
198 * and:
199 * term
200 * term '-a' and
202 static int
203 and ()
205 int value, v2;
207 value = term ();
208 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
210 advance (0);
211 v2 = and ();
212 return (value && v2);
214 return (value);
218 * term - parse a term and return 1 or 0 depending on whether the term
219 * evaluates to true or false, respectively.
221 * term ::=
222 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
223 * '-'('G'|'L'|'O'|'S'|'N') filename
224 * '-t' [int]
225 * '-'('z'|'n') string
226 * '-o' option
227 * string
228 * string ('!='|'='|'==') string
229 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
230 * file '-'(nt|ot|ef) file
231 * '(' <expr> ')'
232 * int ::=
233 * positive and negative integers
235 static int
236 term ()
238 int value;
240 if (pos >= argc)
241 beyond ();
243 /* Deal with leading `not's. */
244 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
246 value = 0;
247 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
249 advance (1);
250 value = 1 - value;
253 return (value ? !term() : term());
256 /* A paren-bracketed argument. */
257 if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
259 advance (1);
260 value = expr ();
261 if (argv[pos] == 0) /* ( */
262 test_syntax_error (_("`)' expected"), (char *)NULL);
263 else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */
264 test_syntax_error (_("`)' expected, found %s"), argv[pos]);
265 advance (0);
266 return (value);
269 /* are there enough arguments left that this could be dyadic? */
270 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
271 value = binary_operator ();
273 /* Might be a switch type argument */
274 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
276 if (test_unop (argv[pos]))
277 value = unary_operator ();
278 else
279 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
281 else
283 value = argv[pos][0] != '\0';
284 advance (0);
287 return (value);
290 static int
291 filecomp (s, t, op)
292 char *s, *t;
293 int op;
295 struct stat st1, st2;
296 int r1, r2;
298 if ((r1 = sh_stat (s, &st1)) < 0)
300 if (op == EF)
301 return (FALSE);
303 if ((r2 = sh_stat (t, &st2)) < 0)
305 if (op == EF)
306 return (FALSE);
309 switch (op)
311 case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
312 case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
313 case EF: return (same_file (s, t, &st1, &st2));
315 return (FALSE);
318 static int
319 arithcomp (s, t, op, flags)
320 char *s, *t;
321 int op, flags;
323 intmax_t l, r;
324 int expok;
326 if (flags & TEST_ARITHEXP)
328 l = evalexp (s, &expok);
329 if (expok == 0)
330 return (FALSE); /* should probably longjmp here */
331 r = evalexp (t, &expok);
332 if (expok == 0)
333 return (FALSE); /* ditto */
335 else
337 if (legal_number (s, &l) == 0)
338 integer_expected_error (s);
339 if (legal_number (t, &r) == 0)
340 integer_expected_error (t);
343 switch (op)
345 case EQ: return (l == r);
346 case NE: return (l != r);
347 case LT: return (l < r);
348 case GT: return (l > r);
349 case LE: return (l <= r);
350 case GE: return (l >= r);
353 return (FALSE);
356 static int
357 patcomp (string, pat, op)
358 char *string, *pat;
359 int op;
361 int m;
363 m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
364 return ((op == EQ) ? (m == 0) : (m != 0));
368 binary_test (op, arg1, arg2, flags)
369 char *op, *arg1, *arg2;
370 int flags;
372 int patmatch;
374 patmatch = (flags & TEST_PATMATCH);
376 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
377 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
379 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
380 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
382 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
383 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
385 else if (op[2] == 't')
387 switch (op[1])
389 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
390 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
391 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
392 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
395 else if (op[1] == 'e')
397 switch (op[2])
399 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
400 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
403 else if (op[2] == 'e')
405 switch (op[1])
407 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
408 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
409 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
413 return (FALSE); /* should never get here */
417 static int
418 binary_operator ()
420 int value;
421 char *w;
423 w = argv[pos + 1];
424 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
425 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
426 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
428 value = binary_test (w, argv[pos], argv[pos + 2], 0);
429 pos += 3;
430 return (value);
433 #if defined (PATTERN_MATCHING)
434 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
436 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
437 pos += 3;
438 return (value);
440 #endif
442 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
444 test_syntax_error (_("%s: binary operator expected"), w);
445 /* NOTREACHED */
446 return (FALSE);
449 value = binary_test (w, argv[pos], argv[pos + 2], 0);
450 pos += 3;
451 return value;
454 static int
455 unary_operator ()
457 char *op;
458 intmax_t r;
460 op = argv[pos];
461 if (test_unop (op) == 0)
462 return (FALSE);
464 /* the only tricky case is `-t', which may or may not take an argument. */
465 if (op[1] == 't')
467 advance (0);
468 if (pos < argc)
470 if (legal_number (argv[pos], &r))
472 advance (0);
473 return (unary_test (op, argv[pos - 1]));
475 else
476 return (FALSE);
478 else
479 return (unary_test (op, "1"));
482 /* All of the unary operators take an argument, so we first call
483 unary_advance (), which checks to make sure that there is an
484 argument, and then advances pos right past it. This means that
485 pos - 1 is the location of the argument. */
486 unary_advance ();
487 return (unary_test (op, argv[pos - 1]));
491 unary_test (op, arg)
492 char *op, *arg;
494 intmax_t r;
495 struct stat stat_buf;
497 switch (op[1])
499 case 'a': /* file exists in the file system? */
500 case 'e':
501 return (sh_stat (arg, &stat_buf) == 0);
503 case 'r': /* file is readable? */
504 return (sh_eaccess (arg, R_OK) == 0);
506 case 'w': /* File is writeable? */
507 return (sh_eaccess (arg, W_OK) == 0);
509 case 'x': /* File is executable? */
510 return (sh_eaccess (arg, X_OK) == 0);
512 case 'O': /* File is owned by you? */
513 return (sh_stat (arg, &stat_buf) == 0 &&
514 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
516 case 'G': /* File is owned by your group? */
517 return (sh_stat (arg, &stat_buf) == 0 &&
518 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
520 case 'N':
521 return (sh_stat (arg, &stat_buf) == 0 &&
522 stat_buf.st_atime <= stat_buf.st_mtime);
524 case 'f': /* File is a file? */
525 if (sh_stat (arg, &stat_buf) < 0)
526 return (FALSE);
528 /* -f is true if the given file exists and is a regular file. */
529 #if defined (S_IFMT)
530 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
531 #else
532 return (S_ISREG (stat_buf.st_mode));
533 #endif /* !S_IFMT */
535 case 'd': /* File is a directory? */
536 return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
538 case 's': /* File has something in it? */
539 return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
541 case 'S': /* File is a socket? */
542 #if !defined (S_ISSOCK)
543 return (FALSE);
544 #else
545 return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
546 #endif /* S_ISSOCK */
548 case 'c': /* File is character special? */
549 return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
551 case 'b': /* File is block special? */
552 return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
554 case 'p': /* File is a named pipe? */
555 #ifndef S_ISFIFO
556 return (FALSE);
557 #else
558 return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
559 #endif /* S_ISFIFO */
561 case 'L': /* Same as -h */
562 case 'h': /* File is a symbolic link? */
563 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
564 return (FALSE);
565 #else
566 return ((arg[0] != '\0') &&
567 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
568 #endif /* S_IFLNK && HAVE_LSTAT */
570 case 'u': /* File is setuid? */
571 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
573 case 'g': /* File is setgid? */
574 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
576 case 'k': /* File has sticky bit set? */
577 #if !defined (S_ISVTX)
578 /* This is not Posix, and is not defined on some Posix systems. */
579 return (FALSE);
580 #else
581 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
582 #endif
584 case 't': /* File fd is a terminal? */
585 if (legal_number (arg, &r) == 0)
586 return (FALSE);
587 return ((r == (int)r) && isatty ((int)r));
589 case 'n': /* True if arg has some length. */
590 return (arg[0] != '\0');
592 case 'z': /* True if arg has no length. */
593 return (arg[0] == '\0');
595 case 'o': /* True if option `arg' is set. */
596 return (minus_o_option_value (arg) == 1);
599 /* We can't actually get here, but this shuts up gcc. */
600 return (FALSE);
603 /* Return TRUE if OP is one of the test command's binary operators. */
605 test_binop (op)
606 char *op;
608 if (op[0] == '=' && op[1] == '\0')
609 return (1); /* '=' */
610 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
611 return (1);
612 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
613 return (1); /* `==' and `!=' */
614 #if defined (PATTERN_MATCHING)
615 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
616 return (1);
617 #endif
618 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
619 return (0);
620 else
622 if (op[2] == 't')
623 switch (op[1])
625 case 'n': /* -nt */
626 case 'o': /* -ot */
627 case 'l': /* -lt */
628 case 'g': /* -gt */
629 return (1);
630 default:
631 return (0);
633 else if (op[1] == 'e')
634 switch (op[2])
636 case 'q': /* -eq */
637 case 'f': /* -ef */
638 return (1);
639 default:
640 return (0);
642 else if (op[2] == 'e')
643 switch (op[1])
645 case 'n': /* -ne */
646 case 'g': /* -ge */
647 case 'l': /* -le */
648 return (1);
649 default:
650 return (0);
652 else
653 return (0);
657 /* Return non-zero if OP is one of the test command's unary operators. */
659 test_unop (op)
660 char *op;
662 if (op[0] != '-' || op[2] != 0)
663 return (0);
665 switch (op[1])
667 case 'a': case 'b': case 'c': case 'd': case 'e':
668 case 'f': case 'g': case 'h': case 'k': case 'n':
669 case 'o': case 'p': case 'r': case 's': case 't':
670 case 'u': case 'w': case 'x': case 'z':
671 case 'G': case 'L': case 'O': case 'S': case 'N':
672 return (1);
675 return (0);
678 static int
679 two_arguments ()
681 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
682 return (argv[pos + 1][0] == '\0');
683 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
685 if (test_unop (argv[pos]))
686 return (unary_operator ());
687 else
688 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
690 else
691 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
693 return (0);
696 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
698 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
699 POSIX requires that `-t' be given an argument. */
700 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
702 static int
703 three_arguments ()
705 int value;
707 if (test_binop (argv[pos+1]))
709 value = binary_operator ();
710 pos = argc;
712 else if (ANDOR (argv[pos+1]))
714 if (argv[pos+1][1] == 'a')
715 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
716 else
717 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
718 pos = argc;
720 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
722 advance (1);
723 value = !two_arguments ();
725 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
727 value = ONE_ARG_TEST(argv[pos+1]);
728 pos = argc;
730 else
731 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
733 return (value);
736 /* This is an implementation of a Posix.2 proposal by David Korn. */
737 static int
738 posixtest ()
740 int value;
742 switch (argc - 1) /* one extra passed in */
744 case 0:
745 value = FALSE;
746 pos = argc;
747 break;
749 case 1:
750 value = ONE_ARG_TEST(argv[1]);
751 pos = argc;
752 break;
754 case 2:
755 value = two_arguments ();
756 pos = argc;
757 break;
759 case 3:
760 value = three_arguments ();
761 break;
763 case 4:
764 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
766 advance (1);
767 value = !three_arguments ();
768 break;
770 /* FALLTHROUGH */
771 default:
772 value = expr ();
775 return (value);
779 * [:
780 * '[' expr ']'
781 * test:
782 * test expr
785 test_command (margc, margv)
786 int margc;
787 char **margv;
789 int value;
790 int code;
792 USE_VAR(margc);
794 code = setjmp (test_exit_buf);
796 if (code)
797 return (test_error_return);
799 argv = margv;
801 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
803 --margc;
805 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
806 test_syntax_error (_("missing `]'"), (char *)NULL);
808 if (margc < 2)
809 test_exit (SHELL_BOOLEAN (FALSE));
812 argc = margc;
813 pos = 1;
815 if (pos >= argc)
816 test_exit (SHELL_BOOLEAN (FALSE));
818 noeval = 0;
819 value = posixtest ();
821 if (pos != argc)
822 test_syntax_error (_("too many arguments"), (char *)NULL);
824 test_exit (SHELL_BOOLEAN (value));