1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell by bfox. */
5 /* Copyright (C) 1987-2004 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
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Define TEST_STANDALONE to get the /bin/test version. Otherwise, you get
24 the shell builtin version. */
28 #include <sys/types.h>
30 #define TEST_STANDALONE 1
36 /* The official name of this program (e.g., no `g' prefix). */
38 # define PROGRAM_NAME "["
40 # define PROGRAM_NAME "test"
45 #include "euidaccess.h"
48 #ifndef _POSIX_VERSION
49 # include <sys/param.h>
50 #endif /* _POSIX_VERSION */
51 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
52 #define digit(c) ((c) >= '0' && (c) <= '9')
53 #define digit_value(c) ((c) - '0')
57 #if !defined (_POSIX_VERSION)
58 # include <sys/file.h>
59 #endif /* !_POSIX_VERSION */
61 extern gid_t getegid ();
62 extern uid_t geteuid ();
64 /* The following few defines control the truth and false output of each stage.
65 TRUE and FALSE are what we use to compute the final output value.
66 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
67 TRUTH_OR is how to do logical or with TRUE and FALSE.
68 TRUTH_AND is how to do logical and with TRUE and FALSE..
69 Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
70 SHELL_BOOLEAN = (!value). */
73 #define SHELL_BOOLEAN(value) (!(value))
74 #define TRUTH_OR(a, b) ((a) | (b))
75 #define TRUTH_AND(a, b) ((a) & (b))
77 /* Exit status for syntax errors, etc. */
78 enum { TEST_FAILURE = 2 };
80 #if defined (TEST_STANDALONE)
81 # define test_exit(val) exit (val)
83 static jmp_buf test_exit_buf;
84 static int test_error_return = 0;
85 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
86 #endif /* !TEST_STANDALONE */
88 static int pos; /* The offset of the current argument in ARGV. */
89 static int argc; /* The number of arguments present in ARGV. */
90 static char **argv; /* The argument list. */
92 static int test_unop (char const *s);
93 static int binop (char *s);
94 static int unary_operator (void);
95 static int binary_operator (bool);
96 static int two_arguments (void);
97 static int three_arguments (void);
98 static int posixtest (int);
100 static int expr (void);
101 static int term (void);
102 static int and (void);
103 static int or (void);
105 static void test_syntax_error (char const *format, char const *arg)
107 static void beyond (void) ATTRIBUTE_NORETURN;
110 test_syntax_error (char const *format, char const *arg)
112 fprintf (stderr, "%s: ", argv[0]);
113 fprintf (stderr, format, arg);
115 test_exit (TEST_FAILURE);
118 /* Increment our position in the argument list. Check that we're not
119 past the end of the argument list. This check is supressed if the
120 argument is FALSE. Made a macro for efficiency. */
125 if ((f) && pos >= argc) \
130 #if !defined (advance)
136 if (f && pos >= argc)
141 #define unary_advance() \
150 * beyond - call when we're beyond the end of the argument list (an
156 test_syntax_error (_("missing argument after %s"), quote (argv[argc - 1]));
159 /* Syntax error for when an integer argument was expected, but
160 something else was found. */
162 integer_expected_error (char const *pch)
164 test_syntax_error (_("%s: integer expression expected\n"), pch);
167 /* Return nonzero if the characters pointed to by STRING constitute a
168 valid number. Stuff the converted number into RESULT if RESULT is
171 isint (register char *string, intmax_t *result)
182 /* Skip leading whitespace characters. */
183 while (whitespace (*string))
189 /* We allow leading `-' or `+'. */
190 if (*string == '-' || *string == '+')
192 if (!digit (string[1]))
201 while (digit (*string))
204 value = (value * 10) + digit_value (*string);
208 /* Skip trailing whitespace, if any. */
209 while (whitespace (*string))
212 /* Error if not at end of string. */
225 /* Find the modification time of FILE, and stuff it into *AGE.
226 Return 0 if successful, -1 if not. */
228 age_of (char *filename, time_t *age)
231 int r = stat (filename, &finfo);
233 *age = finfo.st_mtime;
238 * term - parse a term and return 1 or 0 depending on whether the term
239 * evaluates to true or false, respectively.
242 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
243 * '-'('L'|'x') filename
245 * '-'('z'|'n') string
247 * string ('!='|'=') string
248 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
249 * file '-'(nt|ot|ef) file
253 * positive and negative integers
263 /* Deal with leading `not's. */
264 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
267 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
273 return (value ? !term() : term());
276 /* A paren-bracketed argument. */
277 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
284 pos + nargs < argc && ! STREQ (argv[pos + nargs], ")");
292 value = posixtest (nargs);
294 test_syntax_error (_("')' expected\n"), NULL);
296 if (argv[pos][0] != ')' || argv[pos][1])
297 test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
302 /* are there enough arguments left that this could be dyadic? */
303 if (pos + 4 <= argc && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))
304 value = binary_operator (true);
305 else if (pos + 3 <= argc && binop (argv[pos + 1]))
306 value = binary_operator (false);
308 /* Might be a switch type argument */
309 else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
311 if (test_unop (argv[pos]))
312 value = unary_operator ();
314 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
318 value = (argv[pos][0] != '\0');
326 binary_operator (bool l_is_l)
329 struct stat stat_buf, stat_spare;
332 /* Is the right integer expression of the form '-l string'? */
339 if ((op < argc - 2) && STREQ (argv[op + 1], "-l"))
347 if (argv[op][0] == '-')
349 /* check for eq, nt, and stuff */
356 if (argv[op][2] == 't' && !argv[op][3])
360 l = strlen (argv[op - 1]);
363 if (!isint (argv[op - 1], &l))
364 integer_expected_error (_("before -lt"));
368 r = strlen (argv[op + 2]);
371 if (!isint (argv[op + 1], &r))
372 integer_expected_error (_("after -lt"));
375 return (TRUE == (l < r));
378 if (argv[op][2] == 'e' && !argv[op][3])
382 l = strlen (argv[op - 1]);
385 if (!isint (argv[op - 1], &l))
386 integer_expected_error (_("before -le"));
389 r = strlen (argv[op + 2]);
392 if (!isint (argv[op + 1], &r))
393 integer_expected_error (_("after -le"));
396 return (TRUE == (l <= r));
401 if (argv[op][2] == 't' && !argv[op][3])
403 /* gt integer greater than */
405 l = strlen (argv[op - 1]);
408 if (!isint (argv[op - 1], &l))
409 integer_expected_error (_("before -gt"));
412 r = strlen (argv[op + 2]);
415 if (!isint (argv[op + 1], &r))
416 integer_expected_error (_("after -gt"));
419 return (TRUE == (l > r));
422 if (argv[op][2] == 'e' && !argv[op][3])
424 /* ge - integer greater than or equal to */
426 l = strlen (argv[op - 1]);
429 if (!isint (argv[op - 1], &l))
430 integer_expected_error (_("before -ge"));
433 r = strlen (argv[op + 2]);
436 if (!isint (argv[op + 1], &r))
437 integer_expected_error (_("after -ge"));
440 return (TRUE == (l >= r));
445 if (argv[op][2] == 't' && !argv[op][3])
447 /* nt - newer than */
451 if (l_is_l || r_is_l)
452 test_syntax_error (_("-nt does not accept -l\n"), NULL);
453 le = age_of (argv[op - 1], <);
454 re = age_of (argv[op + 1], &rt);
455 return le > re || (le == 0 && lt > rt);
458 if (argv[op][2] == 'e' && !argv[op][3])
460 /* ne - integer not equal */
462 l = strlen (argv[op - 1]);
465 if (!isint (argv[op - 1], &l))
466 integer_expected_error (_("before -ne"));
469 r = strlen (argv[op + 2]);
472 if (!isint (argv[op + 1], &r))
473 integer_expected_error (_("after -ne"));
476 return (TRUE == (l != r));
481 if (argv[op][2] == 'q' && !argv[op][3])
483 /* eq - integer equal */
485 l = strlen (argv[op - 1]);
488 if (!isint (argv[op - 1], &l))
489 integer_expected_error (_("before -eq"));
492 r = strlen (argv[op + 2]);
495 if (!isint (argv[op + 1], &r))
496 integer_expected_error (_("after -eq"));
499 return (TRUE == (l == r));
502 if (argv[op][2] == 'f' && !argv[op][3])
504 /* ef - hard link? */
506 if (l_is_l || r_is_l)
507 test_syntax_error (_("-ef does not accept -l\n"), NULL);
508 if (stat (argv[op - 1], &stat_buf) < 0)
510 if (stat (argv[op + 1], &stat_spare) < 0)
513 (stat_buf.st_dev == stat_spare.st_dev &&
514 stat_buf.st_ino == stat_spare.st_ino));
519 if ('t' == argv[op][2] && '\000' == argv[op][3])
521 /* ot - older than */
525 if (l_is_l || r_is_l)
526 test_syntax_error (_("-ot does not accept -l\n"), NULL);
527 le = age_of (argv[op - 1], <);
528 re = age_of (argv[op + 1], &rt);
529 return le < re || (re == 0 && lt < rt);
534 /* FIXME: is this dead code? */
535 test_syntax_error (_("unknown binary operator\n"), argv[op]);
538 if (argv[op][0] == '=' && !argv[op][1])
540 value = STREQ (argv[pos], argv[pos + 2]);
542 return (TRUE == value);
545 if (STREQ (argv[op], "!="))
547 value = !STREQ (argv[pos], argv[pos + 2]);
549 return (TRUE == value);
557 unary_operator (void)
560 struct stat stat_buf;
562 switch (argv[pos][1])
567 /* All of the following unary operators use unary_advance (), which
568 checks to make sure that there is an argument, and then advances
569 pos right past it. This means that pos - 1 is the location of the
572 case 'a': /* file exists in the file system? */
575 value = -1 != stat (argv[pos - 1], &stat_buf);
576 return (TRUE == value);
578 case 'r': /* file is readable? */
580 value = -1 != euidaccess (argv[pos - 1], R_OK);
581 return (TRUE == value);
583 case 'w': /* File is writable? */
585 value = -1 != euidaccess (argv[pos - 1], W_OK);
586 return (TRUE == value);
588 case 'x': /* File is executable? */
590 value = -1 != euidaccess (argv[pos - 1], X_OK);
591 return (TRUE == value);
593 case 'O': /* File is owned by you? */
595 if (stat (argv[pos - 1], &stat_buf) < 0)
598 return (TRUE == (geteuid () == stat_buf.st_uid));
600 case 'G': /* File is owned by your group? */
602 if (stat (argv[pos - 1], &stat_buf) < 0)
605 return (TRUE == (getegid () == stat_buf.st_gid));
607 case 'f': /* File is a file? */
609 if (stat (argv[pos - 1], &stat_buf) < 0)
612 /* Under POSIX, -f is true if the given file exists
613 and is a regular file. */
614 return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
615 (0 == (stat_buf.st_mode & S_IFMT))));
617 case 'd': /* File is a directory? */
619 if (stat (argv[pos - 1], &stat_buf) < 0)
622 return (TRUE == (S_ISDIR (stat_buf.st_mode)));
624 case 's': /* File has something in it? */
626 if (stat (argv[pos - 1], &stat_buf) < 0)
629 return (TRUE == (stat_buf.st_size > (off_t) 0));
631 case 'S': /* File is a socket? */
632 #if !defined (S_ISSOCK)
637 if (stat (argv[pos - 1], &stat_buf) < 0)
640 return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
641 #endif /* S_ISSOCK */
643 case 'c': /* File is character special? */
645 if (stat (argv[pos - 1], &stat_buf) < 0)
648 return (TRUE == (S_ISCHR (stat_buf.st_mode)));
650 case 'b': /* File is block special? */
652 if (stat (argv[pos - 1], &stat_buf) < 0)
655 return (TRUE == (S_ISBLK (stat_buf.st_mode)));
657 case 'p': /* File is a named pipe? */
662 if (stat (argv[pos - 1], &stat_buf) < 0)
664 return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
665 #endif /* S_ISFIFO */
667 case 'L': /* Same as -h */
670 case 'h': /* File is a symbolic link? */
675 /* An empty filename is not a valid pathname. */
676 if ((argv[pos - 1][0] == '\0') ||
677 (lstat (argv[pos - 1], &stat_buf) < 0))
680 return (TRUE == (S_ISLNK (stat_buf.st_mode)));
683 case 'u': /* File is setuid? */
688 if (stat (argv[pos - 1], &stat_buf) < 0)
691 return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
694 case 'g': /* File is setgid? */
699 if (stat (argv[pos - 1], &stat_buf) < 0)
702 return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
705 case 'k': /* File has sticky bit set? */
707 if (stat (argv[pos - 1], &stat_buf) < 0)
710 /* This is not Posix, and is not defined on some Posix systems. */
713 return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
716 case 't': /* File (fd) is a terminal? */
720 if (!isint (argv[pos - 1], &fd))
721 integer_expected_error (_("after -t"));
722 return (TRUE == (fd == (int) fd && isatty (fd)));
725 case 'n': /* True if arg has some length. */
727 return (TRUE == (argv[pos - 1][0] != 0));
729 case 'z': /* True if arg has no length. */
731 return (TRUE == (argv[pos - 1][0] == '\0'));
746 while ((pos < argc) && STREQ (argv[pos], "-a"))
749 value = TRUTH_AND (value, and ());
751 return (TRUE == value);
766 while ((pos < argc) && STREQ (argv[pos], "-o"))
769 value = TRUTH_OR (value, or ());
772 return (TRUE == value);
785 return (FALSE ^ (or ())); /* Same with this. */
788 /* Return TRUE if S is one of the test command's binary operators. */
792 return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "-nt")) ||
793 (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
794 (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
795 (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
798 /* Return nonzero if OP is one of the test command's unary operators. */
800 test_unop (char const *op)
807 case 'a': case 'b': case 'c': case 'd': case 'e':
808 case 'f': case 'g': case 'h': case 'k': case 'n':
809 case 'o': case 'p': case 'r': case 's': case 't':
810 case 'u': case 'w': case 'x': case 'z':
811 case 'G': case 'L': case 'O': case 'S': case 'N':
821 return argv[pos++][0] != '\0';
829 if (STREQ (argv[pos], "!"))
832 value = ! one_argument ();
834 else if (argv[pos][0] == '-'
835 && argv[pos][1] != '\0'
836 && argv[pos][2] == '\0')
838 if (test_unop (argv[pos]))
839 value = unary_operator ();
841 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
849 three_arguments (void)
853 if (binop (argv[pos + 1]))
854 value = binary_operator (false);
855 else if (STREQ (argv[pos], "!"))
858 value = !two_arguments ();
860 else if (STREQ (argv[pos], "(") && STREQ (argv[pos + 2], ")"))
863 value = one_argument ();
866 else if (STREQ (argv[pos + 1], "-a") || STREQ (argv[pos + 1], "-o"))
869 test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
873 /* This is an implementation of a Posix.2 proposal by David Korn. */
875 posixtest (int nargs)
882 value = one_argument ();
886 value = two_arguments ();
890 value = three_arguments ();
894 if (STREQ (argv[pos], "!"))
897 value = !three_arguments ();
900 if (STREQ (argv[pos], "(") && STREQ (argv[pos + 3], ")"))
903 value = two_arguments ();
918 #if defined (TEST_STANDALONE)
919 # include "long-options.h"
924 if (status != EXIT_SUCCESS)
925 fprintf (stderr, _("Try `%s --help' for more information.\n"),
930 Usage: test EXPRESSION\n\
931 or: [ EXPRESSION ]\n\
933 Exit with the status determined by EXPRESSION.\n\
936 fputs (HELP_OPTION_DESCRIPTION, stdout);
937 fputs (VERSION_OPTION_DESCRIPTION, stdout);
940 EXPRESSION is true or false and sets exit status. It is one of:\n\
944 ( EXPRESSION ) EXPRESSION is true\n\
945 ! EXPRESSION EXPRESSION is false\n\
946 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
947 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
951 [-n] STRING the length of STRING is nonzero\n\
952 -z STRING the length of STRING is zero\n\
953 STRING1 = STRING2 the strings are equal\n\
954 STRING1 != STRING2 the strings are not equal\n\
958 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
959 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
960 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
961 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
962 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
963 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
967 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
968 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
969 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
973 -b FILE FILE exists and is block special\n\
974 -c FILE FILE exists and is character special\n\
975 -d FILE FILE exists and is a directory\n\
976 -e FILE FILE exists\n\
979 -f FILE FILE exists and is a regular file\n\
980 -g FILE FILE exists and is set-group-ID\n\
981 -h FILE FILE exists and is a symbolic link (same as -L)\n\
982 -G FILE FILE exists and is owned by the effective group ID\n\
983 -k FILE FILE exists and has its sticky bit set\n\
986 -L FILE FILE exists and is a symbolic link (same as -h)\n\
987 -O FILE FILE exists and is owned by the effective user ID\n\
988 -p FILE FILE exists and is a named pipe\n\
989 -r FILE FILE exists and is readable\n\
990 -s FILE FILE exists and has a size greater than zero\n\
993 -S FILE FILE exists and is a socket\n\
994 -t FD file descriptor FD is opened on a terminal\n\
995 -u FILE FILE exists and its set-user-ID bit is set\n\
996 -w FILE FILE exists and is writable\n\
997 -x FILE FILE exists and is executable\n\
1001 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
1002 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
1004 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1008 #endif /* TEST_STANDALONE */
1010 #if !defined (TEST_STANDALONE)
1011 # define main test_command
1014 #define AUTHORS "Kevin Braunsdorf", "Matthew Bradburn"
1023 main (int margc, char **margv)
1027 #if !defined (TEST_STANDALONE)
1030 code = setjmp (test_exit_buf);
1033 return (test_error_return);
1034 #else /* TEST_STANDALONE */
1035 initialize_main (&margc, &margv);
1036 program_name = margv[0];
1037 setlocale (LC_ALL, "");
1038 bindtextdomain (PACKAGE, LOCALEDIR);
1039 textdomain (PACKAGE);
1041 initialize_exit_failure (TEST_FAILURE);
1042 atexit (close_stdout);
1043 #endif /* TEST_STANDALONE */
1049 /* Recognize --help or --version, but only when invoked in the
1050 "[" form, and when the last argument is not "]". POSIX
1051 allows "[ --help" and "[ --version" to have the usual GNU
1052 behavior, but it requires "test --help" and "test --version"
1053 to exit silently with status 1. */
1054 if (margc < 2 || !STREQ (margv[margc - 1], "]"))
1056 parse_long_options (margc, margv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1057 usage, AUTHORS, (char const *) NULL);
1058 test_syntax_error (_("missing `]'\n"), NULL);
1068 test_exit (SHELL_BOOLEAN (FALSE));
1070 value = posixtest (argc - 1);
1073 test_syntax_error (_("extra argument %s"), quote (argv[pos]));
1075 test_exit (SHELL_BOOLEAN (value));