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
37 #include "euidaccess.h"
40 #ifndef _POSIX_VERSION
41 # include <sys/param.h>
42 #endif /* _POSIX_VERSION */
43 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
44 #define digit(c) ((c) >= '0' && (c) <= '9')
45 #define digit_value(c) ((c) - '0')
49 #if !defined (_POSIX_VERSION)
50 # include <sys/file.h>
51 #endif /* !_POSIX_VERSION */
59 # define member(c, s) ((c) ? (strchr ((s), (c)) ? 1 : 0) : 0)
62 extern gid_t getegid ();
63 extern uid_t geteuid ();
72 /* This name is used solely when printing --version information. */
73 #define PROGRAM_NAME "test"
75 /* The following few defines control the truth and false output of each stage.
76 TRUE and FALSE are what we use to compute the final output value.
77 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
78 TRUTH_OR is how to do logical or with TRUE and FALSE.
79 TRUTH_AND is how to do logical and with TRUE and FALSE..
80 Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
81 SHELL_BOOLEAN = (!value). */
84 #define SHELL_BOOLEAN(value) (!(value))
85 #define TRUTH_OR(a, b) ((a) | (b))
86 #define TRUTH_AND(a, b) ((a) & (b))
88 #if defined (TEST_STANDALONE)
89 # define test_exit(val) exit (val)
91 static jmp_buf test_exit_buf;
92 static int test_error_return = 0;
93 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
94 #endif /* !TEST_STANDALONE */
96 static int pos; /* The offset of the current argument in ARGV. */
97 static int argc; /* The number of arguments present in ARGV. */
98 static char **argv; /* The argument list. */
100 static int unop (int op);
101 static int binop (char *s);
102 static int unary_operator (void);
103 static int binary_operator (void);
104 static int two_arguments (void);
105 static int three_arguments (void);
106 static int posixtest (void);
108 static int expr (void);
109 static int term (void);
110 static int and (void);
111 static int or (void);
113 static void test_syntax_error (char const *format, char const *arg)
115 static void beyond (void) ATTRIBUTE_NORETURN;
118 test_syntax_error (char const *format, char const *arg)
120 fprintf (stderr, "%s: ", argv[0]);
121 fprintf (stderr, format, arg);
123 test_exit (SHELL_BOOLEAN (FALSE));
126 #if HAVE_SETREUID && HAVE_SETREGID
127 /* Do the same thing access(2) does, but use the effective uid and gid. */
130 eaccess (char const *file, int mode)
133 static uid_t uid, euid;
134 static gid_t gid, egid;
146 /* Set the real user and group IDs to the effective ones. */
148 setreuid (euid, uid);
150 setregid (egid, gid);
152 result = access (file, mode);
156 setreuid (uid, euid);
158 setregid (gid, egid);
163 # define eaccess(F, M) euidaccess (F, M)
166 /* Increment our position in the argument list. Check that we're not
167 past the end of the argument list. This check is supressed if the
168 argument is FALSE. Made a macro for efficiency. */
173 if ((f) && pos >= argc) \
178 #if !defined (advance)
184 if (f && pos >= argc)
189 #define unary_advance() \
198 * beyond - call when we're beyond the end of the argument list (an
204 test_syntax_error (_("argument expected\n"), NULL);
207 /* Syntax error for when an integer argument was expected, but
208 something else was found. */
210 integer_expected_error (char const *pch)
212 test_syntax_error (_("integer expression expected %s\n"), pch);
215 /* Return nonzero if the characters pointed to by STRING constitute a
216 valid number. Stuff the converted number into RESULT if RESULT is
219 isint (register char *string, intmax_t *result)
230 /* Skip leading whitespace characters. */
231 while (whitespace (*string))
237 /* We allow leading `-' or `+'. */
238 if (*string == '-' || *string == '+')
240 if (!digit (string[1]))
249 while (digit (*string))
252 value = (value * 10) + digit_value (*string);
256 /* Skip trailing whitespace, if any. */
257 while (whitespace (*string))
260 /* Error if not at end of string. */
273 /* Find the modification time of FILE, and stuff it into *AGE.
274 Return 0 if successful, -1 if not. */
276 age_of (char *filename, time_t *age)
279 int r = stat (filename, &finfo);
281 *age = finfo.st_mtime;
286 * term - parse a term and return 1 or 0 depending on whether the term
287 * evaluates to true or false, respectively.
290 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
291 * '-'('L'|'x') filename
293 * '-'('z'|'n') string
295 * string ('!='|'=') string
296 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
297 * file '-'(nt|ot|ef) file
301 * positive and negative integers
311 /* Deal with leading "not"'s. */
312 if ('!' == argv[pos][0] && '\000' == argv[pos][1])
315 while (pos < argc && '!' == argv[pos][0] && '\000' == argv[pos][1])
321 return (value ^ (term ()));
324 /* A paren-bracketed argument. */
325 if (argv[pos][0] == '(' && !argv[pos][1])
330 test_syntax_error (_("')' expected\n"), NULL);
332 if (argv[pos][0] != ')' || argv[pos][1])
333 test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
335 return (TRUE == (value));
338 /* are there enough arguments left that this could be dyadic? */
339 if (((pos + 3 <= argc) && binop (argv[pos + 1])) ||
340 ((pos + 4 <= argc && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))))
341 value = binary_operator ();
343 /* Might be a switch type argument */
344 else if ('-' == argv[pos][0] && argv[pos][1] && 0 == argv[pos][2])
346 if (unop (argv[pos][1]))
347 value = unary_operator ();
349 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
353 value = (argv[pos][0] != '\0');
361 binary_operator (void)
364 struct stat stat_buf, stat_spare;
367 /* Are the left and right integer expressions of the form '-l string'? */
370 if (strcmp (argv[pos], "-l") == 0)
375 /* Make sure that OP is still a valid binary operator. */
376 if ((op >= argc - 1) || (binop (argv[op]) == 0))
377 test_syntax_error (_("%s: binary operator expected\n"), argv[op]);
387 if ((op < argc - 2) && (strcmp (argv[op + 1], "-l") == 0))
395 if (argv[op][0] == '-')
397 /* check for eq, nt, and stuff */
404 if (argv[op][2] == 't' && !argv[op][3])
408 l = strlen (argv[op - 1]);
411 if (!isint (argv[op - 1], &l))
412 integer_expected_error (_("before -lt"));
416 r = strlen (argv[op + 2]);
419 if (!isint (argv[op + 1], &r))
420 integer_expected_error (_("after -lt"));
423 return (TRUE == (l < r));
426 if (argv[op][2] == 'e' && !argv[op][3])
430 l = strlen (argv[op - 1]);
433 if (!isint (argv[op - 1], &l))
434 integer_expected_error (_("before -le"));
437 r = strlen (argv[op + 2]);
440 if (!isint (argv[op + 1], &r))
441 integer_expected_error (_("after -le"));
444 return (TRUE == (l <= r));
449 if (argv[op][2] == 't' && !argv[op][3])
451 /* gt integer greater than */
453 l = strlen (argv[op - 1]);
456 if (!isint (argv[op - 1], &l))
457 integer_expected_error (_("before -gt"));
460 r = strlen (argv[op + 2]);
463 if (!isint (argv[op + 1], &r))
464 integer_expected_error (_("after -gt"));
467 return (TRUE == (l > r));
470 if (argv[op][2] == 'e' && !argv[op][3])
472 /* ge - integer greater than or equal to */
474 l = strlen (argv[op - 1]);
477 if (!isint (argv[op - 1], &l))
478 integer_expected_error (_("before -ge"));
481 r = strlen (argv[op + 2]);
484 if (!isint (argv[op + 1], &r))
485 integer_expected_error (_("after -ge"));
488 return (TRUE == (l >= r));
493 if (argv[op][2] == 't' && !argv[op][3])
495 /* nt - newer than */
499 if (l_is_l || r_is_l)
500 test_syntax_error (_("-nt does not accept -l\n"), NULL);
501 le = age_of (argv[op - 1], <);
502 re = age_of (argv[op + 1], &rt);
503 return le > re || (le == 0 && lt > rt);
506 if (argv[op][2] == 'e' && !argv[op][3])
508 /* ne - integer not equal */
510 l = strlen (argv[op - 1]);
513 if (!isint (argv[op - 1], &l))
514 integer_expected_error (_("before -ne"));
517 r = strlen (argv[op + 2]);
520 if (!isint (argv[op + 1], &r))
521 integer_expected_error (_("after -ne"));
524 return (TRUE == (l != r));
529 if (argv[op][2] == 'q' && !argv[op][3])
531 /* eq - integer equal */
533 l = strlen (argv[op - 1]);
536 if (!isint (argv[op - 1], &l))
537 integer_expected_error (_("before -eq"));
540 r = strlen (argv[op + 2]);
543 if (!isint (argv[op + 1], &r))
544 integer_expected_error (_("after -eq"));
547 return (TRUE == (l == r));
550 if (argv[op][2] == 'f' && !argv[op][3])
552 /* ef - hard link? */
554 if (l_is_l || r_is_l)
555 test_syntax_error (_("-ef does not accept -l\n"), NULL);
556 if (stat (argv[op - 1], &stat_buf) < 0)
558 if (stat (argv[op + 1], &stat_spare) < 0)
561 (stat_buf.st_dev == stat_spare.st_dev &&
562 stat_buf.st_ino == stat_spare.st_ino));
567 if ('t' == argv[op][2] && '\000' == argv[op][3])
569 /* ot - older than */
573 if (l_is_l || r_is_l)
574 test_syntax_error (_("-ot does not accept -l\n"), NULL);
575 le = age_of (argv[op - 1], <);
576 re = age_of (argv[op + 1], &rt);
577 return le < re || (re == 0 && lt < rt);
581 test_syntax_error (_("unknown binary operator"), argv[op]);
584 if (argv[op][0] == '=' && !argv[op][1])
586 value = (strcmp (argv[pos], argv[pos + 2]) == 0);
588 return (TRUE == value);
591 if (strcmp (argv[op], "!=") == 0)
593 value = (strcmp (argv[pos], argv[pos + 2]) != 0);
595 return (TRUE == value);
603 unary_operator (void)
606 struct stat stat_buf;
608 switch (argv[pos][1])
613 /* All of the following unary operators use unary_advance (), which
614 checks to make sure that there is an argument, and then advances
615 pos right past it. This means that pos - 1 is the location of the
618 case 'a': /* file exists in the file system? */
621 value = -1 != stat (argv[pos - 1], &stat_buf);
622 return (TRUE == value);
624 case 'r': /* file is readable? */
626 value = -1 != eaccess (argv[pos - 1], R_OK);
627 return (TRUE == value);
629 case 'w': /* File is writable? */
631 value = -1 != eaccess (argv[pos - 1], W_OK);
632 return (TRUE == value);
634 case 'x': /* File is executable? */
636 value = -1 != eaccess (argv[pos - 1], X_OK);
637 return (TRUE == value);
639 case 'O': /* File is owned by you? */
641 if (stat (argv[pos - 1], &stat_buf) < 0)
644 return (TRUE == (geteuid () == stat_buf.st_uid));
646 case 'G': /* File is owned by your group? */
648 if (stat (argv[pos - 1], &stat_buf) < 0)
651 return (TRUE == (getegid () == stat_buf.st_gid));
653 case 'f': /* File is a file? */
655 if (stat (argv[pos - 1], &stat_buf) < 0)
658 /* Under POSIX, -f is true if the given file exists
659 and is a regular file. */
660 return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
661 (0 == (stat_buf.st_mode & S_IFMT))));
663 case 'd': /* File is a directory? */
665 if (stat (argv[pos - 1], &stat_buf) < 0)
668 return (TRUE == (S_ISDIR (stat_buf.st_mode)));
670 case 's': /* File has something in it? */
672 if (stat (argv[pos - 1], &stat_buf) < 0)
675 return (TRUE == (stat_buf.st_size > (off_t) 0));
677 case 'S': /* File is a socket? */
678 #if !defined (S_ISSOCK)
683 if (stat (argv[pos - 1], &stat_buf) < 0)
686 return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
687 #endif /* S_ISSOCK */
689 case 'c': /* File is character special? */
691 if (stat (argv[pos - 1], &stat_buf) < 0)
694 return (TRUE == (S_ISCHR (stat_buf.st_mode)));
696 case 'b': /* File is block special? */
698 if (stat (argv[pos - 1], &stat_buf) < 0)
701 return (TRUE == (S_ISBLK (stat_buf.st_mode)));
703 case 'p': /* File is a named pipe? */
708 if (stat (argv[pos - 1], &stat_buf) < 0)
710 return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
711 #endif /* S_ISFIFO */
713 case 'L': /* Same as -h */
716 case 'h': /* File is a symbolic link? */
721 /* An empty filename is not a valid pathname. */
722 if ((argv[pos - 1][0] == '\0') ||
723 (lstat (argv[pos - 1], &stat_buf) < 0))
726 return (TRUE == (S_ISLNK (stat_buf.st_mode)));
729 case 'u': /* File is setuid? */
734 if (stat (argv[pos - 1], &stat_buf) < 0)
737 return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
740 case 'g': /* File is setgid? */
745 if (stat (argv[pos - 1], &stat_buf) < 0)
748 return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
751 case 'k': /* File has sticky bit set? */
753 if (stat (argv[pos - 1], &stat_buf) < 0)
756 /* This is not Posix, and is not defined on some Posix systems. */
759 return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
762 case 't': /* File (fd) is a terminal? */
768 if (!isint (argv[pos], &fd))
769 integer_expected_error (_("after -t"));
776 return (TRUE == (fd == (int) fd && isatty (fd)));
779 case 'n': /* True if arg has some length. */
781 return (TRUE == (argv[pos - 1][0] != 0));
783 case 'z': /* True if arg has no length. */
785 return (TRUE == (argv[pos - 1][0] == '\0'));
800 while ((pos < argc) && strcmp (argv[pos], "-a") == 0)
803 value = TRUTH_AND (value, and ());
805 return (TRUE == value);
820 while ((pos < argc) && strcmp (argv[pos], "-o") == 0)
823 value = TRUTH_OR (value, or ());
826 return (TRUE == value);
839 return (FALSE ^ (or ())); /* Same with this. */
842 /* Return TRUE if S is one of the test command's binary operators. */
846 return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "-nt")) ||
847 (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
848 (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
849 (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
852 /* Return nonzero if OP is one of the test command's unary operators. */
856 return (member (op, "abcdefgkLhprsStuwxOGnz"));
860 one_argument (const char *s)
863 return (TRUE == (isatty (1)));
865 return strlen (s) != 0;
873 if (STREQ (argv[pos], "!"))
874 value = ! one_argument (argv[pos+1]);
875 else if (argv[pos][0] == '-'
876 && argv[pos][1] != '\0'
877 && argv[pos][2] == '\0')
879 if (unop (argv[pos][1]))
880 value = unary_operator ();
882 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
890 three_arguments (void)
894 if (STREQ (argv[pos], "!"))
897 value = !two_arguments ();
899 else if (binop (argv[pos+1]))
901 value = binary_operator ();
904 else if ((STREQ (argv[pos+1], "-a")) || (STREQ (argv[pos+1], "-o")) ||
905 (argv[pos][0] == '('))
908 test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
912 /* This is an implementation of a Posix.2 proposal by David Korn. */
918 switch (argc - 1) /* one extra passed in */
926 value = one_argument (argv[1]);
931 value = two_arguments ();
936 value = three_arguments ();
940 if (STREQ (argv[pos], "!"))
943 value = !three_arguments ();
955 #if defined (TEST_STANDALONE)
956 # include "long-options.h"
957 # include "closeout.h"
963 fprintf (stderr, _("Try `%s --help' for more information.\n"),
968 Usage: %s EXPRESSION\n\
969 or: [ EXPRESSION ]\n\
972 program_name, program_name);
974 Exit with the status determined by EXPRESSION.\n\
977 fputs (HELP_OPTION_DESCRIPTION, stdout);
978 fputs (VERSION_OPTION_DESCRIPTION, stdout);
981 EXPRESSION is true or false and sets exit status. It is one of:\n\
985 ( EXPRESSION ) EXPRESSION is true\n\
986 ! EXPRESSION EXPRESSION is false\n\
987 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
988 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
992 [-n] STRING the length of STRING is nonzero\n\
993 -z STRING the length of STRING is zero\n\
994 STRING1 = STRING2 the strings are equal\n\
995 STRING1 != STRING2 the strings are not equal\n\
999 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
1000 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
1001 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
1002 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
1003 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
1004 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
1008 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
1009 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
1010 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
1014 -b FILE FILE exists and is block special\n\
1015 -c FILE FILE exists and is character special\n\
1016 -d FILE FILE exists and is a directory\n\
1017 -e FILE FILE exists\n\
1020 -f FILE FILE exists and is a regular file\n\
1021 -g FILE FILE exists and is set-group-ID\n\
1022 -h FILE FILE exists and is a symbolic link (same as -L)\n\
1023 -G FILE FILE exists and is owned by the effective group ID\n\
1024 -k FILE FILE exists and has its sticky bit set\n\
1027 -L FILE FILE exists and is a symbolic link (same as -h)\n\
1028 -O FILE FILE exists and is owned by the effective user ID\n\
1029 -p FILE FILE exists and is a named pipe\n\
1030 -r FILE FILE exists and is readable\n\
1031 -s FILE FILE exists and has a size greater than zero\n\
1034 -S FILE FILE exists and is a socket\n\
1035 -t [FD] file descriptor FD (stdout by default) is opened on a terminal\n\
1036 -u FILE FILE exists and its set-user-ID bit is set\n\
1037 -w FILE FILE exists and is writable\n\
1038 -x FILE FILE exists and is executable\n\
1042 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
1043 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
1045 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1049 #endif /* TEST_STANDALONE */
1051 #if !defined (TEST_STANDALONE)
1052 # define main test_command
1055 #define AUTHORS N_ ("FIXME: ksb and mjb")
1064 main (int margc, char **margv)
1068 #if !defined (TEST_STANDALONE)
1071 code = setjmp (test_exit_buf);
1074 return (test_error_return);
1075 #else /* TEST_STANDALONE */
1076 initialize_main (&margc, &margv);
1077 program_name = margv[0];
1078 setlocale (LC_ALL, "");
1079 bindtextdomain (PACKAGE, LOCALEDIR);
1080 textdomain (PACKAGE);
1082 atexit (close_stdout);
1083 #endif /* TEST_STANDALONE */
1087 if (margv[0] && strcmp (margv[0], "[") == 0)
1089 /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
1090 if (getenv ("POSIXLY_CORRECT") == NULL)
1091 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1097 test_exit (SHELL_BOOLEAN (FALSE));
1099 if (margv[margc] && strcmp (margv[margc], "]") != 0)
1100 test_syntax_error (_("missing `]'\n"), NULL);
1107 test_exit (SHELL_BOOLEAN (FALSE));
1109 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
1111 value = posixtest ();
1114 test_syntax_error (_("too many arguments\n"), NULL);
1116 test_exit (SHELL_BOOLEAN (value));