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. */
28 #include <sys/types.h>
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "test"
33 #define TEST_STANDALONE 1
41 #include "euidaccess.h"
43 #ifndef _POSIX_VERSION
44 # include <sys/param.h>
45 #endif /* _POSIX_VERSION */
46 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
47 #define digit(c) ((c) >= '0' && (c) <= '9')
48 #define digit_value(c) ((c) - '0')
52 #if !defined (_POSIX_VERSION)
53 # include <sys/file.h>
54 #endif /* !_POSIX_VERSION */
56 extern gid_t getegid ();
57 extern uid_t geteuid ();
66 /* This name is used solely when printing --version information. */
67 #define PROGRAM_NAME "test"
69 /* The following few defines control the truth and false output of each stage.
70 TRUE and FALSE are what we use to compute the final output value.
71 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
72 TRUTH_OR is how to do logical or with TRUE and FALSE.
73 TRUTH_AND is how to do logical and with TRUE and FALSE..
74 Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
75 SHELL_BOOLEAN = (!value). */
78 #define SHELL_BOOLEAN(value) (!(value))
79 #define TRUTH_OR(a, b) ((a) | (b))
80 #define TRUTH_AND(a, b) ((a) & (b))
82 #if defined (TEST_STANDALONE)
83 # define test_exit(val) exit (val)
85 static jmp_buf test_exit_buf;
86 static int test_error_return = 0;
87 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
88 #endif /* !TEST_STANDALONE */
90 static int pos; /* The offset of the current argument in ARGV. */
91 static int argc; /* The number of arguments present in ARGV. */
92 static char **argv; /* The argument list. */
94 static int test_unop (char const *s);
95 static int binop (char *s);
96 static int unary_operator (void);
97 static int binary_operator (void);
98 static int two_arguments (void);
99 static int three_arguments (void);
100 static int posixtest (void);
102 static int expr (void);
103 static int term (void);
104 static int and (void);
105 static int or (void);
107 static void test_syntax_error (char const *format, char const *arg)
109 static void beyond (void) ATTRIBUTE_NORETURN;
112 test_syntax_error (char const *format, char const *arg)
114 fprintf (stderr, "%s: ", argv[0]);
115 fprintf (stderr, format, arg);
117 test_exit (SHELL_BOOLEAN (FALSE));
120 #if HAVE_SETREUID && HAVE_SETREGID
121 /* Do the same thing access(2) does, but use the effective uid and gid. */
124 eaccess (char const *file, int mode)
127 static uid_t uid, euid;
128 static gid_t gid, egid;
140 /* Set the real user and group IDs to the effective ones. */
142 setreuid (euid, uid);
144 setregid (egid, gid);
146 result = access (file, mode);
150 setreuid (uid, euid);
152 setregid (gid, egid);
157 # define eaccess(F, M) euidaccess (F, M)
160 /* Increment our position in the argument list. Check that we're not
161 past the end of the argument list. This check is supressed if the
162 argument is FALSE. Made a macro for efficiency. */
167 if ((f) && pos >= argc) \
172 #if !defined (advance)
178 if (f && pos >= argc)
183 #define unary_advance() \
192 * beyond - call when we're beyond the end of the argument list (an
198 test_syntax_error (_("argument expected\n"), NULL);
201 /* Syntax error for when an integer argument was expected, but
202 something else was found. */
204 integer_expected_error (char const *pch)
206 test_syntax_error (_("%s: integer expression expected\n"), pch);
209 /* Return nonzero if the characters pointed to by STRING constitute a
210 valid number. Stuff the converted number into RESULT if RESULT is
213 isint (register char *string, intmax_t *result)
224 /* Skip leading whitespace characters. */
225 while (whitespace (*string))
231 /* We allow leading `-' or `+'. */
232 if (*string == '-' || *string == '+')
234 if (!digit (string[1]))
243 while (digit (*string))
246 value = (value * 10) + digit_value (*string);
250 /* Skip trailing whitespace, if any. */
251 while (whitespace (*string))
254 /* Error if not at end of string. */
267 /* Find the modification time of FILE, and stuff it into *AGE.
268 Return 0 if successful, -1 if not. */
270 age_of (char *filename, time_t *age)
273 int r = stat (filename, &finfo);
275 *age = finfo.st_mtime;
280 * term - parse a term and return 1 or 0 depending on whether the term
281 * evaluates to true or false, respectively.
284 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
285 * '-'('L'|'x') filename
287 * '-'('z'|'n') string
289 * string ('!='|'=') string
290 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
291 * file '-'(nt|ot|ef) file
295 * positive and negative integers
305 /* Deal with leading `not's. */
306 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
309 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
315 return (value ? !term() : term());
318 /* A paren-bracketed argument. */
319 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
324 test_syntax_error (_("')' expected\n"), NULL);
326 if (argv[pos][0] != ')' || argv[pos][1])
327 test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
332 /* are there enough arguments left that this could be dyadic? */
333 if (((pos + 3 <= argc) && binop (argv[pos + 1])) ||
334 ((pos + 4 <= argc && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))))
335 value = binary_operator ();
337 /* Might be a switch type argument */
338 else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
340 if (test_unop (argv[pos]))
341 value = unary_operator ();
343 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
347 value = (argv[pos][0] != '\0');
355 binary_operator (void)
358 struct stat stat_buf, stat_spare;
361 /* Are the left and right integer expressions of the form '-l string'? */
364 if (strcmp (argv[pos], "-l") == 0)
369 /* Make sure that OP is still a valid binary operator. */
370 if ((op >= argc - 1) || (binop (argv[op]) == 0))
371 test_syntax_error (_("%s: binary operator expected\n"), argv[op]);
381 if ((op < argc - 2) && (strcmp (argv[op + 1], "-l") == 0))
389 if (argv[op][0] == '-')
391 /* check for eq, nt, and stuff */
398 if (argv[op][2] == 't' && !argv[op][3])
402 l = strlen (argv[op - 1]);
405 if (!isint (argv[op - 1], &l))
406 integer_expected_error (_("before -lt"));
410 r = strlen (argv[op + 2]);
413 if (!isint (argv[op + 1], &r))
414 integer_expected_error (_("after -lt"));
417 return (TRUE == (l < r));
420 if (argv[op][2] == 'e' && !argv[op][3])
424 l = strlen (argv[op - 1]);
427 if (!isint (argv[op - 1], &l))
428 integer_expected_error (_("before -le"));
431 r = strlen (argv[op + 2]);
434 if (!isint (argv[op + 1], &r))
435 integer_expected_error (_("after -le"));
438 return (TRUE == (l <= r));
443 if (argv[op][2] == 't' && !argv[op][3])
445 /* gt integer greater than */
447 l = strlen (argv[op - 1]);
450 if (!isint (argv[op - 1], &l))
451 integer_expected_error (_("before -gt"));
454 r = strlen (argv[op + 2]);
457 if (!isint (argv[op + 1], &r))
458 integer_expected_error (_("after -gt"));
461 return (TRUE == (l > r));
464 if (argv[op][2] == 'e' && !argv[op][3])
466 /* ge - integer greater than or equal to */
468 l = strlen (argv[op - 1]);
471 if (!isint (argv[op - 1], &l))
472 integer_expected_error (_("before -ge"));
475 r = strlen (argv[op + 2]);
478 if (!isint (argv[op + 1], &r))
479 integer_expected_error (_("after -ge"));
482 return (TRUE == (l >= r));
487 if (argv[op][2] == 't' && !argv[op][3])
489 /* nt - newer than */
493 if (l_is_l || r_is_l)
494 test_syntax_error (_("-nt does not accept -l\n"), NULL);
495 le = age_of (argv[op - 1], <);
496 re = age_of (argv[op + 1], &rt);
497 return le > re || (le == 0 && lt > rt);
500 if (argv[op][2] == 'e' && !argv[op][3])
502 /* ne - integer not equal */
504 l = strlen (argv[op - 1]);
507 if (!isint (argv[op - 1], &l))
508 integer_expected_error (_("before -ne"));
511 r = strlen (argv[op + 2]);
514 if (!isint (argv[op + 1], &r))
515 integer_expected_error (_("after -ne"));
518 return (TRUE == (l != r));
523 if (argv[op][2] == 'q' && !argv[op][3])
525 /* eq - integer equal */
527 l = strlen (argv[op - 1]);
530 if (!isint (argv[op - 1], &l))
531 integer_expected_error (_("before -eq"));
534 r = strlen (argv[op + 2]);
537 if (!isint (argv[op + 1], &r))
538 integer_expected_error (_("after -eq"));
541 return (TRUE == (l == r));
544 if (argv[op][2] == 'f' && !argv[op][3])
546 /* ef - hard link? */
548 if (l_is_l || r_is_l)
549 test_syntax_error (_("-ef does not accept -l\n"), NULL);
550 if (stat (argv[op - 1], &stat_buf) < 0)
552 if (stat (argv[op + 1], &stat_spare) < 0)
555 (stat_buf.st_dev == stat_spare.st_dev &&
556 stat_buf.st_ino == stat_spare.st_ino));
561 if ('t' == argv[op][2] && '\000' == argv[op][3])
563 /* ot - older than */
567 if (l_is_l || r_is_l)
568 test_syntax_error (_("-ot does not accept -l\n"), NULL);
569 le = age_of (argv[op - 1], <);
570 re = age_of (argv[op + 1], &rt);
571 return le < re || (re == 0 && lt < rt);
576 /* FIXME: is this dead code? */
577 test_syntax_error (_("unknown binary operator\n"), argv[op]);
580 if (argv[op][0] == '=' && !argv[op][1])
582 value = (strcmp (argv[pos], argv[pos + 2]) == 0);
584 return (TRUE == value);
587 if (strcmp (argv[op], "!=") == 0)
589 value = (strcmp (argv[pos], argv[pos + 2]) != 0);
591 return (TRUE == value);
599 unary_operator (void)
602 struct stat stat_buf;
604 switch (argv[pos][1])
609 /* All of the following unary operators use unary_advance (), which
610 checks to make sure that there is an argument, and then advances
611 pos right past it. This means that pos - 1 is the location of the
614 case 'a': /* file exists in the file system? */
617 value = -1 != stat (argv[pos - 1], &stat_buf);
618 return (TRUE == value);
620 case 'r': /* file is readable? */
622 value = -1 != eaccess (argv[pos - 1], R_OK);
623 return (TRUE == value);
625 case 'w': /* File is writable? */
627 value = -1 != eaccess (argv[pos - 1], W_OK);
628 return (TRUE == value);
630 case 'x': /* File is executable? */
632 value = -1 != eaccess (argv[pos - 1], X_OK);
633 return (TRUE == value);
635 case 'O': /* File is owned by you? */
637 if (stat (argv[pos - 1], &stat_buf) < 0)
640 return (TRUE == (geteuid () == stat_buf.st_uid));
642 case 'G': /* File is owned by your group? */
644 if (stat (argv[pos - 1], &stat_buf) < 0)
647 return (TRUE == (getegid () == stat_buf.st_gid));
649 case 'f': /* File is a file? */
651 if (stat (argv[pos - 1], &stat_buf) < 0)
654 /* Under POSIX, -f is true if the given file exists
655 and is a regular file. */
656 return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
657 (0 == (stat_buf.st_mode & S_IFMT))));
659 case 'd': /* File is a directory? */
661 if (stat (argv[pos - 1], &stat_buf) < 0)
664 return (TRUE == (S_ISDIR (stat_buf.st_mode)));
666 case 's': /* File has something in it? */
668 if (stat (argv[pos - 1], &stat_buf) < 0)
671 return (TRUE == (stat_buf.st_size > (off_t) 0));
673 case 'S': /* File is a socket? */
674 #if !defined (S_ISSOCK)
679 if (stat (argv[pos - 1], &stat_buf) < 0)
682 return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
683 #endif /* S_ISSOCK */
685 case 'c': /* File is character special? */
687 if (stat (argv[pos - 1], &stat_buf) < 0)
690 return (TRUE == (S_ISCHR (stat_buf.st_mode)));
692 case 'b': /* File is block special? */
694 if (stat (argv[pos - 1], &stat_buf) < 0)
697 return (TRUE == (S_ISBLK (stat_buf.st_mode)));
699 case 'p': /* File is a named pipe? */
704 if (stat (argv[pos - 1], &stat_buf) < 0)
706 return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
707 #endif /* S_ISFIFO */
709 case 'L': /* Same as -h */
712 case 'h': /* File is a symbolic link? */
717 /* An empty filename is not a valid pathname. */
718 if ((argv[pos - 1][0] == '\0') ||
719 (lstat (argv[pos - 1], &stat_buf) < 0))
722 return (TRUE == (S_ISLNK (stat_buf.st_mode)));
725 case 'u': /* File is setuid? */
730 if (stat (argv[pos - 1], &stat_buf) < 0)
733 return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
736 case 'g': /* File is setgid? */
741 if (stat (argv[pos - 1], &stat_buf) < 0)
744 return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
747 case 'k': /* File has sticky bit set? */
749 if (stat (argv[pos - 1], &stat_buf) < 0)
752 /* This is not Posix, and is not defined on some Posix systems. */
755 return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
758 case 't': /* File (fd) is a terminal? */
764 if (!isint (argv[pos], &fd))
765 integer_expected_error (_("after -t"));
772 return (TRUE == (fd == (int) fd && isatty (fd)));
775 case 'n': /* True if arg has some length. */
777 return (TRUE == (argv[pos - 1][0] != 0));
779 case 'z': /* True if arg has no length. */
781 return (TRUE == (argv[pos - 1][0] == '\0'));
796 while ((pos < argc) && strcmp (argv[pos], "-a") == 0)
799 value = TRUTH_AND (value, and ());
801 return (TRUE == value);
816 while ((pos < argc) && strcmp (argv[pos], "-o") == 0)
819 value = TRUTH_OR (value, or ());
822 return (TRUE == value);
835 return (FALSE ^ (or ())); /* Same with this. */
838 /* Return TRUE if S is one of the test command's binary operators. */
842 return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "-nt")) ||
843 (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
844 (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
845 (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
848 /* Return nonzero if OP is one of the test command's unary operators. */
850 test_unop (char const *op)
857 case 'a': case 'b': case 'c': case 'd': case 'e':
858 case 'f': case 'g': case 'h': case 'k': case 'n':
859 case 'o': case 'p': case 'r': case 's': case 't':
860 case 'u': case 'w': case 'x': case 'z':
861 case 'G': case 'L': case 'O': case 'S': case 'N':
869 one_argument (const char *s)
871 if (! getenv ("POSIXLY_CORRECT") && STREQ (s, "-t"))
872 return (TRUE == (isatty (1)));
874 return strlen (s) != 0;
882 if (STREQ (argv[pos], "!"))
883 value = ! one_argument (argv[pos+1]);
884 else if (argv[pos][0] == '-'
885 && argv[pos][1] != '\0'
886 && argv[pos][2] == '\0')
888 if (test_unop (argv[pos]))
889 value = unary_operator ();
891 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
899 three_arguments (void)
903 if (STREQ (argv[pos], "!"))
906 value = !two_arguments ();
908 else if (binop (argv[pos+1]))
910 value = binary_operator ();
913 else if ((STREQ (argv[pos+1], "-a")) || (STREQ (argv[pos+1], "-o")) ||
914 (argv[pos][0] == '('))
917 test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
921 /* This is an implementation of a Posix.2 proposal by David Korn. */
927 switch (argc - 1) /* one extra passed in */
935 value = one_argument (argv[1]);
940 value = two_arguments ();
945 value = three_arguments ();
949 if (STREQ (argv[pos], "!"))
952 value = !three_arguments ();
964 #if defined (TEST_STANDALONE)
965 # include "long-options.h"
971 fprintf (stderr, _("Try `%s --help' for more information.\n"),
976 Usage: %s EXPRESSION\n\
977 or: [ EXPRESSION ]\n\
980 program_name, program_name);
982 Exit with the status determined by EXPRESSION.\n\
985 fputs (HELP_OPTION_DESCRIPTION, stdout);
986 fputs (VERSION_OPTION_DESCRIPTION, stdout);
989 EXPRESSION is true or false and sets exit status. It is one of:\n\
993 ( EXPRESSION ) EXPRESSION is true\n\
994 ! EXPRESSION EXPRESSION is false\n\
995 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
996 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
1000 [-n] STRING the length of STRING is nonzero\n\
1001 -z STRING the length of STRING is zero\n\
1002 STRING1 = STRING2 the strings are equal\n\
1003 STRING1 != STRING2 the strings are not equal\n\
1007 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
1008 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
1009 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
1010 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
1011 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
1012 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
1016 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
1017 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
1018 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
1022 -b FILE FILE exists and is block special\n\
1023 -c FILE FILE exists and is character special\n\
1024 -d FILE FILE exists and is a directory\n\
1025 -e FILE FILE exists\n\
1028 -f FILE FILE exists and is a regular file\n\
1029 -g FILE FILE exists and is set-group-ID\n\
1030 -h FILE FILE exists and is a symbolic link (same as -L)\n\
1031 -G FILE FILE exists and is owned by the effective group ID\n\
1032 -k FILE FILE exists and has its sticky bit set\n\
1035 -L FILE FILE exists and is a symbolic link (same as -h)\n\
1036 -O FILE FILE exists and is owned by the effective user ID\n\
1037 -p FILE FILE exists and is a named pipe\n\
1038 -r FILE FILE exists and is readable\n\
1039 -s FILE FILE exists and has a size greater than zero\n\
1042 -S FILE FILE exists and is a socket\n\
1043 -t [FD] file descriptor FD (stdout by default) is opened on a terminal\n\
1044 -u FILE FILE exists and its set-user-ID bit is set\n\
1045 -w FILE FILE exists and is writable\n\
1046 -x FILE FILE exists and is executable\n\
1050 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
1051 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
1053 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1057 #endif /* TEST_STANDALONE */
1059 #if !defined (TEST_STANDALONE)
1060 # define main test_command
1063 #define AUTHORS N_ ("Kevin Braunsdorf and Matthew Bradburn")
1072 main (int margc, char **margv)
1076 #if !defined (TEST_STANDALONE)
1079 code = setjmp (test_exit_buf);
1082 return (test_error_return);
1083 #else /* TEST_STANDALONE */
1084 initialize_main (&margc, &margv);
1085 program_name = margv[0];
1086 setlocale (LC_ALL, "");
1087 bindtextdomain (PACKAGE, LOCALEDIR);
1088 textdomain (PACKAGE);
1090 atexit (close_stdout);
1091 #endif /* TEST_STANDALONE */
1093 /* Recognize --help or --version unless POSIXLY_CORRECT is set. */
1094 if (! getenv ("POSIXLY_CORRECT"))
1095 parse_long_options (margc, margv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1104 if (margc < 1 || strcmp (margv[margc], "]") != 0)
1105 test_syntax_error (_("missing `]'\n"), NULL);
1112 test_exit (SHELL_BOOLEAN (FALSE));
1114 value = posixtest ();
1117 test_syntax_error (_("too many arguments\n"), NULL);
1119 test_exit (SHELL_BOOLEAN (value));