1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell by bfox. */
5 /* Copyright (C) 1987-2003 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. */
25 /* #define TEST_STANDALONE */
29 #include <sys/types.h>
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "test"
34 #define TEST_STANDALONE 1
36 #if !defined (TEST_STANDALONE)
38 # include "posixstat.h"
39 # include "filecntl.h"
40 #else /* TEST_STANDALONE */
43 # include "euidaccess.h"
44 # if !defined (S_IXUGO)
47 # if defined (_POSIX_VERSION)
49 # else /* !_POSIX_VERSION */
50 # include <sys/param.h>
51 # endif /* _POSIX_VERSION */
52 # define whitespace(c) (((c) == ' ') || ((c) == '\t'))
53 # define digit(c) ((c) >= '0' && (c) <= '9')
54 # define digit_value(c) ((c) - '0')
56 #endif /* TEST_STANDALONE */
58 #if !defined (_POSIX_VERSION)
59 # include <sys/file.h>
60 #endif /* !_POSIX_VERSION */
68 #define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
71 # define member(c, s) ((c) ? (strchr ((s), (c)) ? 1 : 0) : 0)
74 extern gid_t getegid ();
75 extern uid_t geteuid ();
84 /* This name is used solely when printing --version information. */
85 #define PROGRAM_NAME "test"
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 TRUTH_OR is how to do logical or with TRUE and FALSE.
91 TRUTH_AND is how to do logical and with TRUE and FALSE..
92 Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
93 SHELL_BOOLEAN = (!value). */
96 #define SHELL_BOOLEAN(value) (!(value))
97 #define TRUTH_OR(a, b) ((a) | (b))
98 #define TRUTH_AND(a, b) ((a) & (b))
100 #if defined (TEST_STANDALONE)
101 # define test_exit(val) exit (val)
103 static jmp_buf test_exit_buf;
104 static int test_error_return = 0;
105 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
106 #endif /* !TEST_STANDALONE */
108 static int pos; /* The offset of the current argument in ARGV. */
109 static int argc; /* The number of arguments present in ARGV. */
110 static char **argv; /* The argument list. */
112 static int unop (int op);
113 static int binop (char *s);
114 static int unary_operator (void);
115 static int binary_operator (void);
116 static int two_arguments (void);
117 static int three_arguments (void);
118 static int posixtest (void);
120 static int expr (void);
121 static int term (void);
122 static int and (void);
123 static int or (void);
125 static void test_syntax_error (char const *format, char const *arg)
127 static void beyond (void) ATTRIBUTE_NORETURN;
130 test_syntax_error (char const *format, char const *arg)
132 fprintf (stderr, "%s: ", argv[0]);
133 fprintf (stderr, format, arg);
135 test_exit (SHELL_BOOLEAN (FALSE));
138 #if HAVE_SETREUID && HAVE_SETREGID
139 /* Do the same thing access(2) does, but use the effective uid and gid. */
142 eaccess (char const *file, int mode)
145 static uid_t uid, euid;
146 static gid_t gid, egid;
158 /* Set the real user and group IDs to the effective ones. */
160 setreuid (euid, uid);
162 setregid (egid, gid);
164 result = access (file, mode);
168 setreuid (uid, euid);
170 setregid (gid, egid);
175 # define eaccess(F, M) euidaccess (F, M)
178 /* Increment our position in the argument list. Check that we're not
179 past the end of the argument list. This check is supressed if the
180 argument is FALSE. Made a macro for efficiency. */
185 if ((f) && pos >= argc) \
190 #if !defined (advance)
196 if (f && pos >= argc)
201 #define unary_advance() \
210 * beyond - call when we're beyond the end of the argument list (an
216 test_syntax_error (_("argument expected\n"), NULL);
219 /* Syntax error for when an integer argument was expected, but
220 something else was found. */
222 integer_expected_error (char const *pch)
224 test_syntax_error (_("integer expression expected %s\n"), pch);
227 /* Return nonzero if the characters pointed to by STRING constitute a
228 valid number. Stuff the converted number into RESULT if RESULT is
231 isint (register char *string, intmax_t *result)
242 /* Skip leading whitespace characters. */
243 while (whitespace (*string))
249 /* We allow leading `-' or `+'. */
250 if (*string == '-' || *string == '+')
252 if (!digit (string[1]))
261 while (digit (*string))
264 value = (value * 10) + digit_value (*string);
268 /* Skip trailing whitespace, if any. */
269 while (whitespace (*string))
272 /* Error if not at end of string. */
285 /* Find the modification time of FILE, and stuff it into *AGE.
286 Return 0 if successful, -1 if not. */
288 age_of (char *filename, time_t *age)
291 int r = stat (filename, &finfo);
293 *age = finfo.st_mtime;
298 * term - parse a term and return 1 or 0 depending on whether the term
299 * evaluates to true or false, respectively.
302 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
303 * '-'('L'|'x') filename
305 * '-'('z'|'n') string
307 * string ('!='|'=') string
308 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
309 * file '-'(nt|ot|ef) file
313 * positive and negative integers
323 /* Deal with leading "not"'s. */
324 if ('!' == argv[pos][0] && '\000' == argv[pos][1])
327 while (pos < argc && '!' == argv[pos][0] && '\000' == argv[pos][1])
333 return (value ^ (term ()));
336 /* A paren-bracketed argument. */
337 if (argv[pos][0] == '(' && !argv[pos][1])
342 test_syntax_error (_("')' expected\n"), NULL);
344 if (argv[pos][0] != ')' || argv[pos][1])
345 test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
347 return (TRUE == (value));
350 /* are there enough arguments left that this could be dyadic? */
351 if (((pos + 3 <= argc) && binop (argv[pos + 1])) ||
352 ((pos + 4 <= argc && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))))
353 value = binary_operator ();
355 /* Might be a switch type argument */
356 else if ('-' == argv[pos][0] && argv[pos][1] && 0 == argv[pos][2])
358 if (unop (argv[pos][1]))
359 value = unary_operator ();
361 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
365 value = (argv[pos][0] != '\0');
373 binary_operator (void)
376 struct stat stat_buf, stat_spare;
379 /* Are the left and right integer expressions of the form '-l string'? */
382 if (strcmp (argv[pos], "-l") == 0)
387 /* Make sure that OP is still a valid binary operator. */
388 if ((op >= argc - 1) || (binop (argv[op]) == 0))
389 test_syntax_error (_("%s: binary operator expected\n"), argv[op]);
399 if ((op < argc - 2) && (strcmp (argv[op + 1], "-l") == 0))
407 if (argv[op][0] == '-')
409 /* check for eq, nt, and stuff */
416 if (argv[op][2] == 't' && !argv[op][3])
420 l = strlen (argv[op - 1]);
423 if (!isint (argv[op - 1], &l))
424 integer_expected_error (_("before -lt"));
428 r = strlen (argv[op + 2]);
431 if (!isint (argv[op + 1], &r))
432 integer_expected_error (_("after -lt"));
435 return (TRUE == (l < r));
438 if (argv[op][2] == 'e' && !argv[op][3])
442 l = strlen (argv[op - 1]);
445 if (!isint (argv[op - 1], &l))
446 integer_expected_error (_("before -le"));
449 r = strlen (argv[op + 2]);
452 if (!isint (argv[op + 1], &r))
453 integer_expected_error (_("after -le"));
456 return (TRUE == (l <= r));
461 if (argv[op][2] == 't' && !argv[op][3])
463 /* gt integer greater than */
465 l = strlen (argv[op - 1]);
468 if (!isint (argv[op - 1], &l))
469 integer_expected_error (_("before -gt"));
472 r = strlen (argv[op + 2]);
475 if (!isint (argv[op + 1], &r))
476 integer_expected_error (_("after -gt"));
479 return (TRUE == (l > r));
482 if (argv[op][2] == 'e' && !argv[op][3])
484 /* ge - integer greater than or equal to */
486 l = strlen (argv[op - 1]);
489 if (!isint (argv[op - 1], &l))
490 integer_expected_error (_("before -ge"));
493 r = strlen (argv[op + 2]);
496 if (!isint (argv[op + 1], &r))
497 integer_expected_error (_("after -ge"));
500 return (TRUE == (l >= r));
505 if (argv[op][2] == 't' && !argv[op][3])
507 /* nt - newer than */
511 if (l_is_l || r_is_l)
512 test_syntax_error (_("-nt does not accept -l\n"), NULL);
513 le = age_of (argv[op - 1], <);
514 re = age_of (argv[op + 1], &rt);
515 return le > re || (le == 0 && lt > rt);
518 if (argv[op][2] == 'e' && !argv[op][3])
520 /* ne - integer not equal */
522 l = strlen (argv[op - 1]);
525 if (!isint (argv[op - 1], &l))
526 integer_expected_error (_("before -ne"));
529 r = strlen (argv[op + 2]);
532 if (!isint (argv[op + 1], &r))
533 integer_expected_error (_("after -ne"));
536 return (TRUE == (l != r));
541 if (argv[op][2] == 'q' && !argv[op][3])
543 /* eq - integer equal */
545 l = strlen (argv[op - 1]);
548 if (!isint (argv[op - 1], &l))
549 integer_expected_error (_("before -eq"));
552 r = strlen (argv[op + 2]);
555 if (!isint (argv[op + 1], &r))
556 integer_expected_error (_("after -eq"));
559 return (TRUE == (l == r));
562 if (argv[op][2] == 'f' && !argv[op][3])
564 /* ef - hard link? */
566 if (l_is_l || r_is_l)
567 test_syntax_error (_("-ef does not accept -l\n"), NULL);
568 if (stat (argv[op - 1], &stat_buf) < 0)
570 if (stat (argv[op + 1], &stat_spare) < 0)
573 (stat_buf.st_dev == stat_spare.st_dev &&
574 stat_buf.st_ino == stat_spare.st_ino));
579 if ('t' == argv[op][2] && '\000' == argv[op][3])
581 /* ot - older than */
585 if (l_is_l || r_is_l)
586 test_syntax_error (_("-ot does not accept -l\n"), NULL);
587 le = age_of (argv[op - 1], <);
588 re = age_of (argv[op + 1], &rt);
589 return le < re || (re == 0 && lt < rt);
593 test_syntax_error (_("unknown binary operator"), argv[op]);
596 if (argv[op][0] == '=' && !argv[op][1])
598 value = (strcmp (argv[pos], argv[pos + 2]) == 0);
600 return (TRUE == value);
603 if (strcmp (argv[op], "!=") == 0)
605 value = (strcmp (argv[pos], argv[pos + 2]) != 0);
607 return (TRUE == value);
615 unary_operator (void)
618 struct stat stat_buf;
620 switch (argv[pos][1])
625 /* All of the following unary operators use unary_advance (), which
626 checks to make sure that there is an argument, and then advances
627 pos right past it. This means that pos - 1 is the location of the
630 case 'a': /* file exists in the file system? */
633 value = -1 != stat (argv[pos - 1], &stat_buf);
634 return (TRUE == value);
636 case 'r': /* file is readable? */
638 value = -1 != eaccess (argv[pos - 1], R_OK);
639 return (TRUE == value);
641 case 'w': /* File is writable? */
643 value = -1 != eaccess (argv[pos - 1], W_OK);
644 return (TRUE == value);
646 case 'x': /* File is executable? */
648 value = -1 != eaccess (argv[pos - 1], X_OK);
649 return (TRUE == value);
651 case 'O': /* File is owned by you? */
653 if (stat (argv[pos - 1], &stat_buf) < 0)
656 return (TRUE == (geteuid () == stat_buf.st_uid));
658 case 'G': /* File is owned by your group? */
660 if (stat (argv[pos - 1], &stat_buf) < 0)
663 return (TRUE == (getegid () == stat_buf.st_gid));
665 case 'f': /* File is a file? */
667 if (stat (argv[pos - 1], &stat_buf) < 0)
670 /* Under POSIX, -f is true if the given file exists
671 and is a regular file. */
672 return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
673 (0 == (stat_buf.st_mode & S_IFMT))));
675 case 'd': /* File is a directory? */
677 if (stat (argv[pos - 1], &stat_buf) < 0)
680 return (TRUE == (S_ISDIR (stat_buf.st_mode)));
682 case 's': /* File has something in it? */
684 if (stat (argv[pos - 1], &stat_buf) < 0)
687 return (TRUE == (stat_buf.st_size > (off_t) 0));
689 case 'S': /* File is a socket? */
690 #if !defined (S_ISSOCK)
695 if (stat (argv[pos - 1], &stat_buf) < 0)
698 return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
699 #endif /* S_ISSOCK */
701 case 'c': /* File is character special? */
703 if (stat (argv[pos - 1], &stat_buf) < 0)
706 return (TRUE == (S_ISCHR (stat_buf.st_mode)));
708 case 'b': /* File is block special? */
710 if (stat (argv[pos - 1], &stat_buf) < 0)
713 return (TRUE == (S_ISBLK (stat_buf.st_mode)));
715 case 'p': /* File is a named pipe? */
720 if (stat (argv[pos - 1], &stat_buf) < 0)
722 return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
723 #endif /* S_ISFIFO */
725 case 'L': /* Same as -h */
728 case 'h': /* File is a symbolic link? */
733 /* An empty filename is not a valid pathname. */
734 if ((argv[pos - 1][0] == '\0') ||
735 (lstat (argv[pos - 1], &stat_buf) < 0))
738 return (TRUE == (S_ISLNK (stat_buf.st_mode)));
741 case 'u': /* File is setuid? */
746 if (stat (argv[pos - 1], &stat_buf) < 0)
749 return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
752 case 'g': /* File is setgid? */
757 if (stat (argv[pos - 1], &stat_buf) < 0)
760 return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
763 case 'k': /* File has sticky bit set? */
765 if (stat (argv[pos - 1], &stat_buf) < 0)
768 /* This is not Posix, and is not defined on some Posix systems. */
771 return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
774 case 't': /* File (fd) is a terminal? */
780 if (!isint (argv[pos], &fd))
781 integer_expected_error (_("after -t"));
788 return (TRUE == (fd == (int) fd && isatty (fd)));
791 case 'n': /* True if arg has some length. */
793 return (TRUE == (argv[pos - 1][0] != 0));
795 case 'z': /* True if arg has no length. */
797 return (TRUE == (argv[pos - 1][0] == '\0'));
812 while ((pos < argc) && strcmp (argv[pos], "-a") == 0)
815 value = TRUTH_AND (value, and ());
817 return (TRUE == value);
832 while ((pos < argc) && strcmp (argv[pos], "-o") == 0)
835 value = TRUTH_OR (value, or ());
838 return (TRUE == value);
851 return (FALSE ^ (or ())); /* Same with this. */
854 /* Return TRUE if S is one of the test command's binary operators. */
858 return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "-nt")) ||
859 (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
860 (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
861 (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
864 /* Return nonzero if OP is one of the test command's unary operators. */
868 return (member (op, "abcdefgkLhprsStuwxOGnz"));
872 one_argument (const char *s)
875 return (TRUE == (isatty (1)));
877 return strlen (s) != 0;
885 if (STREQ (argv[pos], "!"))
886 value = ! one_argument (argv[pos+1]);
887 else if (argv[pos][0] == '-'
888 && argv[pos][1] != '\0'
889 && argv[pos][2] == '\0')
891 if (unop (argv[pos][1]))
892 value = unary_operator ();
894 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
902 three_arguments (void)
906 if (STREQ (argv[pos], "!"))
909 value = !two_arguments ();
911 else if (binop (argv[pos+1]))
913 value = binary_operator ();
916 else if ((STREQ (argv[pos+1], "-a")) || (STREQ (argv[pos+1], "-o")) ||
917 (argv[pos][0] == '('))
920 test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
924 /* This is an implementation of a Posix.2 proposal by David Korn. */
930 switch (argc - 1) /* one extra passed in */
938 value = one_argument (argv[1]);
943 value = two_arguments ();
948 value = three_arguments ();
952 if (STREQ (argv[pos], "!"))
955 value = !three_arguments ();
967 #if defined (TEST_STANDALONE)
968 # include "long-options.h"
969 # include "closeout.h"
975 fprintf (stderr, _("Try `%s --help' for more information.\n"),
980 Usage: %s EXPRESSION\n\
981 or: [ EXPRESSION ]\n\
984 program_name, program_name);
986 Exit with the status determined by EXPRESSION.\n\
989 fputs (HELP_OPTION_DESCRIPTION, stdout);
990 fputs (VERSION_OPTION_DESCRIPTION, stdout);
993 EXPRESSION is true or false and sets exit status. It is one of:\n\
997 ( EXPRESSION ) EXPRESSION is true\n\
998 ! EXPRESSION EXPRESSION is false\n\
999 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
1000 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
1004 [-n] STRING the length of STRING is nonzero\n\
1005 -z STRING the length of STRING is zero\n\
1006 STRING1 = STRING2 the strings are equal\n\
1007 STRING1 != STRING2 the strings are not equal\n\
1011 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
1012 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
1013 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
1014 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
1015 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
1016 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
1020 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
1021 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
1022 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
1026 -b FILE FILE exists and is block special\n\
1027 -c FILE FILE exists and is character special\n\
1028 -d FILE FILE exists and is a directory\n\
1029 -e FILE FILE exists\n\
1032 -f FILE FILE exists and is a regular file\n\
1033 -g FILE FILE exists and is set-group-ID\n\
1034 -h FILE FILE exists and is a symbolic link (same as -L)\n\
1035 -G FILE FILE exists and is owned by the effective group ID\n\
1036 -k FILE FILE exists and has its sticky bit set\n\
1039 -L FILE FILE exists and is a symbolic link (same as -h)\n\
1040 -O FILE FILE exists and is owned by the effective user ID\n\
1041 -p FILE FILE exists and is a named pipe\n\
1042 -r FILE FILE exists and is readable\n\
1043 -s FILE FILE exists and has a size greater than zero\n\
1046 -S FILE FILE exists and is a socket\n\
1047 -t [FD] file descriptor FD (stdout by default) is opened on a terminal\n\
1048 -u FILE FILE exists and its set-user-ID bit is set\n\
1049 -w FILE FILE exists and is writable\n\
1050 -x FILE FILE exists and is executable\n\
1054 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
1055 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
1057 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1061 #endif /* TEST_STANDALONE */
1063 #if !defined (TEST_STANDALONE)
1064 # define main test_command
1067 #define AUTHORS N_ ("FIXME: ksb and mjb")
1076 main (int margc, char **margv)
1080 #if !defined (TEST_STANDALONE)
1083 code = setjmp (test_exit_buf);
1086 return (test_error_return);
1087 #else /* TEST_STANDALONE */
1088 program_name = margv[0];
1089 setlocale (LC_ALL, "");
1090 bindtextdomain (PACKAGE, LOCALEDIR);
1091 textdomain (PACKAGE);
1093 atexit (close_stdout);
1094 #endif /* TEST_STANDALONE */
1098 if (margv[0] && strcmp (margv[0], "[") == 0)
1100 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
1101 if (getenv ("POSIXLY_CORRECT") == NULL)
1102 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1108 test_exit (SHELL_BOOLEAN (FALSE));
1110 if (margv[margc] && strcmp (margv[margc], "]") != 0)
1111 test_syntax_error (_("missing `]'\n"), NULL);
1118 test_exit (SHELL_BOOLEAN (FALSE));
1120 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1122 value = posixtest ();
1125 test_syntax_error (_("too many arguments\n"), NULL);
1127 test_exit (SHELL_BOOLEAN (value));