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"
47 #ifndef _POSIX_VERSION
48 # include <sys/param.h>
49 #endif /* _POSIX_VERSION */
50 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
51 #define digit(c) ((c) >= '0' && (c) <= '9')
52 #define digit_value(c) ((c) - '0')
56 #if !defined (_POSIX_VERSION)
57 # include <sys/file.h>
58 #endif /* !_POSIX_VERSION */
60 extern gid_t
getegid ();
61 extern uid_t
geteuid ();
70 /* The following few defines control the truth and false output of each stage.
71 TRUE and FALSE are what we use to compute the final output value.
72 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
73 TRUTH_OR is how to do logical or with TRUE and FALSE.
74 TRUTH_AND is how to do logical and with TRUE and FALSE..
75 Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
76 SHELL_BOOLEAN = (!value). */
79 #define SHELL_BOOLEAN(value) (!(value))
80 #define TRUTH_OR(a, b) ((a) | (b))
81 #define TRUTH_AND(a, b) ((a) & (b))
83 /* Exit status for syntax errors, etc. */
84 enum { TEST_FAILURE
= 2 };
86 #if defined (TEST_STANDALONE)
87 # define test_exit(val) exit (val)
89 static jmp_buf test_exit_buf
;
90 static int test_error_return
= 0;
91 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
92 #endif /* !TEST_STANDALONE */
94 static int pos
; /* The offset of the current argument in ARGV. */
95 static int argc
; /* The number of arguments present in ARGV. */
96 static char **argv
; /* The argument list. */
98 static int test_unop (char const *s
);
99 static int binop (char *s
);
100 static int unary_operator (void);
101 static int binary_operator (bool);
102 static int two_arguments (void);
103 static int three_arguments (void);
104 static int posixtest (int);
106 static int expr (void);
107 static int term (void);
108 static int and (void);
109 static int or (void);
111 static void test_syntax_error (char const *format
, char const *arg
)
113 static void beyond (void) ATTRIBUTE_NORETURN
;
116 test_syntax_error (char const *format
, char const *arg
)
118 fprintf (stderr
, "%s: ", argv
[0]);
119 fprintf (stderr
, format
, arg
);
121 test_exit (TEST_FAILURE
);
124 #if HAVE_SETREUID && HAVE_SETREGID
125 /* Do the same thing access(2) does, but use the effective uid and gid. */
128 eaccess (char const *file
, int mode
)
131 static uid_t uid
, euid
;
132 static gid_t gid
, egid
;
144 /* Set the real user and group IDs to the effective ones. */
146 setreuid (euid
, uid
);
148 setregid (egid
, gid
);
150 result
= access (file
, mode
);
154 setreuid (uid
, euid
);
156 setregid (gid
, egid
);
161 # define eaccess(F, M) euidaccess (F, M)
164 /* Increment our position in the argument list. Check that we're not
165 past the end of the argument list. This check is supressed if the
166 argument is FALSE. Made a macro for efficiency. */
171 if ((f) && pos >= argc) \
176 #if !defined (advance)
182 if (f
&& pos
>= argc
)
187 #define unary_advance() \
196 * beyond - call when we're beyond the end of the argument list (an
202 test_syntax_error (_("argument expected\n"), NULL
);
205 /* Syntax error for when an integer argument was expected, but
206 something else was found. */
208 integer_expected_error (char const *pch
)
210 test_syntax_error (_("%s: integer expression expected\n"), pch
);
213 /* Return nonzero if the characters pointed to by STRING constitute a
214 valid number. Stuff the converted number into RESULT if RESULT is
217 isint (register char *string
, intmax_t *result
)
228 /* Skip leading whitespace characters. */
229 while (whitespace (*string
))
235 /* We allow leading `-' or `+'. */
236 if (*string
== '-' || *string
== '+')
238 if (!digit (string
[1]))
247 while (digit (*string
))
250 value
= (value
* 10) + digit_value (*string
);
254 /* Skip trailing whitespace, if any. */
255 while (whitespace (*string
))
258 /* Error if not at end of string. */
271 /* Find the modification time of FILE, and stuff it into *AGE.
272 Return 0 if successful, -1 if not. */
274 age_of (char *filename
, time_t *age
)
277 int r
= stat (filename
, &finfo
);
279 *age
= finfo
.st_mtime
;
284 * term - parse a term and return 1 or 0 depending on whether the term
285 * evaluates to true or false, respectively.
288 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
289 * '-'('L'|'x') filename
291 * '-'('z'|'n') string
293 * string ('!='|'=') string
294 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
295 * file '-'(nt|ot|ef) file
299 * positive and negative integers
309 /* Deal with leading `not's. */
310 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
313 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
319 return (value
? !term() : term());
322 /* A paren-bracketed argument. */
323 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0')
330 pos
+ nargs
< argc
&& ! STREQ (argv
[pos
+ nargs
], ")");
338 value
= posixtest (nargs
);
340 test_syntax_error (_("')' expected\n"), NULL
);
342 if (argv
[pos
][0] != ')' || argv
[pos
][1])
343 test_syntax_error (_("')' expected, found %s\n"), argv
[pos
]);
348 /* are there enough arguments left that this could be dyadic? */
349 if (pos
+ 4 <= argc
&& STREQ (argv
[pos
], "-l") && binop (argv
[pos
+ 2]))
350 value
= binary_operator (true);
351 else if (pos
+ 3 <= argc
&& binop (argv
[pos
+ 1]))
352 value
= binary_operator (false);
354 /* Might be a switch type argument */
355 else if (argv
[pos
][0] == '-' && argv
[pos
][1] && argv
[pos
][2] == '\0')
357 if (test_unop (argv
[pos
]))
358 value
= unary_operator ();
360 test_syntax_error (_("%s: unary operator expected\n"), argv
[pos
]);
364 value
= (argv
[pos
][0] != '\0');
372 binary_operator (bool l_is_l
)
375 struct stat stat_buf
, stat_spare
;
378 /* Is the right integer expression of the form '-l string'? */
385 if ((op
< argc
- 2) && (strcmp (argv
[op
+ 1], "-l") == 0))
393 if (argv
[op
][0] == '-')
395 /* check for eq, nt, and stuff */
402 if (argv
[op
][2] == 't' && !argv
[op
][3])
406 l
= strlen (argv
[op
- 1]);
409 if (!isint (argv
[op
- 1], &l
))
410 integer_expected_error (_("before -lt"));
414 r
= strlen (argv
[op
+ 2]);
417 if (!isint (argv
[op
+ 1], &r
))
418 integer_expected_error (_("after -lt"));
421 return (TRUE
== (l
< r
));
424 if (argv
[op
][2] == 'e' && !argv
[op
][3])
428 l
= strlen (argv
[op
- 1]);
431 if (!isint (argv
[op
- 1], &l
))
432 integer_expected_error (_("before -le"));
435 r
= strlen (argv
[op
+ 2]);
438 if (!isint (argv
[op
+ 1], &r
))
439 integer_expected_error (_("after -le"));
442 return (TRUE
== (l
<= r
));
447 if (argv
[op
][2] == 't' && !argv
[op
][3])
449 /* gt integer greater than */
451 l
= strlen (argv
[op
- 1]);
454 if (!isint (argv
[op
- 1], &l
))
455 integer_expected_error (_("before -gt"));
458 r
= strlen (argv
[op
+ 2]);
461 if (!isint (argv
[op
+ 1], &r
))
462 integer_expected_error (_("after -gt"));
465 return (TRUE
== (l
> r
));
468 if (argv
[op
][2] == 'e' && !argv
[op
][3])
470 /* ge - integer greater than or equal to */
472 l
= strlen (argv
[op
- 1]);
475 if (!isint (argv
[op
- 1], &l
))
476 integer_expected_error (_("before -ge"));
479 r
= strlen (argv
[op
+ 2]);
482 if (!isint (argv
[op
+ 1], &r
))
483 integer_expected_error (_("after -ge"));
486 return (TRUE
== (l
>= r
));
491 if (argv
[op
][2] == 't' && !argv
[op
][3])
493 /* nt - newer than */
497 if (l_is_l
|| r_is_l
)
498 test_syntax_error (_("-nt does not accept -l\n"), NULL
);
499 le
= age_of (argv
[op
- 1], <
);
500 re
= age_of (argv
[op
+ 1], &rt
);
501 return le
> re
|| (le
== 0 && lt
> rt
);
504 if (argv
[op
][2] == 'e' && !argv
[op
][3])
506 /* ne - integer not equal */
508 l
= strlen (argv
[op
- 1]);
511 if (!isint (argv
[op
- 1], &l
))
512 integer_expected_error (_("before -ne"));
515 r
= strlen (argv
[op
+ 2]);
518 if (!isint (argv
[op
+ 1], &r
))
519 integer_expected_error (_("after -ne"));
522 return (TRUE
== (l
!= r
));
527 if (argv
[op
][2] == 'q' && !argv
[op
][3])
529 /* eq - integer equal */
531 l
= strlen (argv
[op
- 1]);
534 if (!isint (argv
[op
- 1], &l
))
535 integer_expected_error (_("before -eq"));
538 r
= strlen (argv
[op
+ 2]);
541 if (!isint (argv
[op
+ 1], &r
))
542 integer_expected_error (_("after -eq"));
545 return (TRUE
== (l
== r
));
548 if (argv
[op
][2] == 'f' && !argv
[op
][3])
550 /* ef - hard link? */
552 if (l_is_l
|| r_is_l
)
553 test_syntax_error (_("-ef does not accept -l\n"), NULL
);
554 if (stat (argv
[op
- 1], &stat_buf
) < 0)
556 if (stat (argv
[op
+ 1], &stat_spare
) < 0)
559 (stat_buf
.st_dev
== stat_spare
.st_dev
&&
560 stat_buf
.st_ino
== stat_spare
.st_ino
));
565 if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
567 /* ot - older than */
571 if (l_is_l
|| r_is_l
)
572 test_syntax_error (_("-ot does not accept -l\n"), NULL
);
573 le
= age_of (argv
[op
- 1], <
);
574 re
= age_of (argv
[op
+ 1], &rt
);
575 return le
< re
|| (re
== 0 && lt
< rt
);
580 /* FIXME: is this dead code? */
581 test_syntax_error (_("unknown binary operator\n"), 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? */
766 if (!isint (argv
[pos
- 1], &fd
))
767 integer_expected_error (_("after -t"));
768 return (TRUE
== (fd
== (int) fd
&& isatty (fd
)));
771 case 'n': /* True if arg has some length. */
773 return (TRUE
== (argv
[pos
- 1][0] != 0));
775 case 'z': /* True if arg has no length. */
777 return (TRUE
== (argv
[pos
- 1][0] == '\0'));
792 while ((pos
< argc
) && strcmp (argv
[pos
], "-a") == 0)
795 value
= TRUTH_AND (value
, and ());
797 return (TRUE
== value
);
812 while ((pos
< argc
) && strcmp (argv
[pos
], "-o") == 0)
815 value
= TRUTH_OR (value
, or ());
818 return (TRUE
== value
);
831 return (FALSE
^ (or ())); /* Same with this. */
834 /* Return TRUE if S is one of the test command's binary operators. */
838 return ((STREQ (s
, "=")) || (STREQ (s
, "!=")) || (STREQ (s
, "-nt")) ||
839 (STREQ (s
, "-ot")) || (STREQ (s
, "-ef")) || (STREQ (s
, "-eq")) ||
840 (STREQ (s
, "-ne")) || (STREQ (s
, "-lt")) || (STREQ (s
, "-le")) ||
841 (STREQ (s
, "-gt")) || (STREQ (s
, "-ge")));
844 /* Return nonzero if OP is one of the test command's unary operators. */
846 test_unop (char const *op
)
853 case 'a': case 'b': case 'c': case 'd': case 'e':
854 case 'f': case 'g': case 'h': case 'k': case 'n':
855 case 'o': case 'p': case 'r': case 's': case 't':
856 case 'u': case 'w': case 'x': case 'z':
857 case 'G': case 'L': case 'O': case 'S': case 'N':
867 return argv
[pos
++][0] != '\0';
875 if (STREQ (argv
[pos
], "!"))
878 value
= ! one_argument ();
880 else if (argv
[pos
][0] == '-'
881 && argv
[pos
][1] != '\0'
882 && argv
[pos
][2] == '\0')
884 if (test_unop (argv
[pos
]))
885 value
= unary_operator ();
887 test_syntax_error (_("%s: unary operator expected\n"), argv
[pos
]);
895 three_arguments (void)
899 if (binop (argv
[pos
+ 1]))
900 value
= binary_operator (false);
901 else if (STREQ (argv
[pos
], "!"))
904 value
= !two_arguments ();
906 else if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 2], ")"))
909 value
= one_argument ();
912 else if (STREQ (argv
[pos
+ 1], "-a") || STREQ (argv
[pos
+ 1], "-o"))
915 test_syntax_error (_("%s: binary operator expected\n"), argv
[pos
+1]);
919 /* This is an implementation of a Posix.2 proposal by David Korn. */
921 posixtest (int nargs
)
928 value
= one_argument ();
932 value
= two_arguments ();
936 value
= three_arguments ();
940 if (STREQ (argv
[pos
], "!"))
943 value
= !three_arguments ();
946 if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 3], ")"))
949 value
= two_arguments ();
964 #if defined (TEST_STANDALONE)
965 # include "long-options.h"
970 if (status
!= EXIT_SUCCESS
)
971 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
976 Usage: test EXPRESSION\n\
977 or: [ EXPRESSION ]\n\
979 Exit with the status determined by EXPRESSION.\n\
982 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
983 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
986 EXPRESSION is true or false and sets exit status. It is one of:\n\
990 ( EXPRESSION ) EXPRESSION is true\n\
991 ! EXPRESSION EXPRESSION is false\n\
992 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
993 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
997 [-n] STRING the length of STRING is nonzero\n\
998 -z STRING the length of STRING is zero\n\
999 STRING1 = STRING2 the strings are equal\n\
1000 STRING1 != STRING2 the strings are not equal\n\
1004 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
1005 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
1006 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
1007 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
1008 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
1009 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
1013 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
1014 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
1015 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
1019 -b FILE FILE exists and is block special\n\
1020 -c FILE FILE exists and is character special\n\
1021 -d FILE FILE exists and is a directory\n\
1022 -e FILE FILE exists\n\
1025 -f FILE FILE exists and is a regular file\n\
1026 -g FILE FILE exists and is set-group-ID\n\
1027 -h FILE FILE exists and is a symbolic link (same as -L)\n\
1028 -G FILE FILE exists and is owned by the effective group ID\n\
1029 -k FILE FILE exists and has its sticky bit set\n\
1032 -L FILE FILE exists and is a symbolic link (same as -h)\n\
1033 -O FILE FILE exists and is owned by the effective user ID\n\
1034 -p FILE FILE exists and is a named pipe\n\
1035 -r FILE FILE exists and is readable\n\
1036 -s FILE FILE exists and has a size greater than zero\n\
1039 -S FILE FILE exists and is a socket\n\
1040 -t [FD] file descriptor FD (stdout by default) is opened on a terminal\n\
1041 -u FILE FILE exists and its set-user-ID bit is set\n\
1042 -w FILE FILE exists and is writable\n\
1043 -x FILE FILE exists and is executable\n\
1047 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
1048 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
1050 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
1054 #endif /* TEST_STANDALONE */
1056 #if !defined (TEST_STANDALONE)
1057 # define main test_command
1060 #define AUTHORS "Kevin Braunsdorf", "Matthew Bradburn"
1069 main (int margc
, char **margv
)
1073 #if !defined (TEST_STANDALONE)
1076 code
= setjmp (test_exit_buf
);
1079 return (test_error_return
);
1080 #else /* TEST_STANDALONE */
1081 initialize_main (&margc
, &margv
);
1082 program_name
= margv
[0];
1083 setlocale (LC_ALL
, "");
1084 bindtextdomain (PACKAGE
, LOCALEDIR
);
1085 textdomain (PACKAGE
);
1087 initialize_exit_failure (TEST_FAILURE
);
1088 atexit (close_stdout
);
1089 #endif /* TEST_STANDALONE */
1095 /* Recognize --help or --version, but only when invoked in the
1096 "[" form, and when the last argument is not "]". POSIX
1097 allows "[ --help" and "[ --version" to have the usual GNU
1098 behavior, but it requires "test --help" and "test --version"
1099 to exit silently with status 1. */
1100 if (margc
< 2 || strcmp (margv
[margc
- 1], "]") != 0)
1102 parse_long_options (margc
, margv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
1103 usage
, AUTHORS
, (char const *) NULL
);
1104 test_syntax_error (_("missing `]'\n"), NULL
);
1114 test_exit (SHELL_BOOLEAN (FALSE
));
1116 value
= posixtest (argc
- 1);
1119 test_syntax_error (_("too many arguments\n"), NULL
);
1121 test_exit (SHELL_BOOLEAN (value
));