1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987-2005 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 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. */
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));
131 test_syntax_error (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
145 test_syntax_error (_("argument expected"), (char *)NULL
);
148 /* Syntax error for when an integer argument was expected, but
149 something else was found. */
151 integer_expected_error (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)
173 return (FALSE
^ or ()); /* Same with this. */
187 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'o' && !argv
[pos
][2])
191 return (value
|| v2
);
208 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'a' && !argv
[pos
][2])
212 return (value
&& v2
);
218 * term - parse a term and return 1 or 0 depending on whether the term
219 * evaluates to true or false, respectively.
222 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
223 * '-'('G'|'L'|'O'|'S'|'N') filename
225 * '-'('z'|'n') string
228 * string ('!='|'='|'==') string
229 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
230 * file '-'(nt|ot|ef) file
233 * positive and negative integers
243 /* Deal with leading `not's. */
244 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
247 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
253 return (value
? !term() : term());
256 /* A paren-bracketed argument. */
257 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0') /* ) */
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
]);
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 ();
279 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
283 value
= argv
[pos
][0] != '\0';
295 struct stat st1
, st2
;
298 if ((r1
= sh_stat (s
, &st1
)) < 0)
303 if ((r2
= sh_stat (t
, &st2
)) < 0)
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 ((st1
.st_dev
== st2
.st_dev
) && (st1
.st_ino
== st2
.st_ino
));
319 arithcomp (s
, t
, op
, flags
)
326 if (flags
& TEST_ARITHEXP
)
328 l
= evalexp (s
, &expok
);
330 return (FALSE
); /* should probably longjmp here */
331 r
= evalexp (t
, &expok
);
333 return (FALSE
); /* ditto */
337 if (legal_number (s
, &l
) == 0)
338 integer_expected_error (s
);
339 if (legal_number (t
, &r
) == 0)
340 integer_expected_error (t
);
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
);
357 patcomp (string
, pat
, op
)
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
;
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')
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')
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')
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 */
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);
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
);
442 if ((w
[0] != '-' || w
[3] != '\0') || test_binop (w
) == 0)
444 test_syntax_error (_("%s: binary operator expected"), w
);
449 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
461 if (test_unop (op
) == 0)
464 /* the only tricky case is `-t', which may or may not take an argument. */
470 if (legal_number (argv
[pos
], &r
))
473 return (unary_test (op
, argv
[pos
- 1]));
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. */
487 return (unary_test (op
, argv
[pos
- 1]));
495 struct stat stat_buf
;
499 case 'a': /* file exists in the file system? */
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
);
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)
528 /* -f is true if the given file exists and is a regular file. */
530 return (S_ISREG (stat_buf
.st_mode
) || (stat_buf
.st_mode
& S_IFMT
) == 0);
532 return (S_ISREG (stat_buf
.st_mode
));
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)
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? */
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)
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. */
581 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISVTX
) != 0);
584 case 't': /* File fd is a terminal? */
585 if (legal_number (arg
, &r
) == 0)
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. */
603 /* Return TRUE if OP is one of the test command's binary operators. */
608 if (op
[0] == '=' && op
[1] == '\0')
609 return (1); /* '=' */
610 else if ((op
[0] == '<' || op
[0] == '>') && op
[1] == '\0') /* string <, > */
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] == '!'))
618 else if (op
[0] != '-' || op
[2] == '\0' || op
[3] != '\0')
633 else if (op
[1] == 'e')
642 else if (op
[2] == 'e')
657 /* Return non-zero if OP is one of the test command's unary operators. */
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':
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 ());
688 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
691 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
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')
707 if (test_binop (argv
[pos
+1]))
709 value
= binary_operator ();
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]);
717 value
= ONE_ARG_TEST(argv
[pos
]) || ONE_ARG_TEST(argv
[pos
+2]);
720 else if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
723 value
= !two_arguments ();
725 else if (argv
[pos
][0] == '(' && argv
[pos
+2][0] == ')')
727 value
= ONE_ARG_TEST(argv
[pos
+1]);
731 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
736 /* This is an implementation of a Posix.2 proposal by David Korn. */
742 switch (argc
- 1) /* one extra passed in */
750 value
= ONE_ARG_TEST(argv
[1]);
755 value
= two_arguments ();
760 value
= three_arguments ();
764 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
767 value
= !three_arguments ();
785 test_command (margc
, margv
)
794 code
= setjmp (test_exit_buf
);
797 return (test_error_return
);
801 if (margv
[0] && margv
[0][0] == '[' && margv
[0][1] == '\0')
805 if (margv
[margc
] && (margv
[margc
][0] != ']' || margv
[margc
][1]))
806 test_syntax_error (_("missing `]'"), (char *)NULL
);
809 test_exit (SHELL_BOOLEAN (FALSE
));
816 test_exit (SHELL_BOOLEAN (FALSE
));
819 value
= posixtest ();
822 test_syntax_error (_("too many arguments"), (char *)NULL
);
824 test_exit (SHELL_BOOLEAN (value
));