1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell by bfox. */
5 /* Copyright (C) 1987-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include "stat-time.h"
49 #include "strnumcmp.h"
52 # include <sys/param.h>
57 /* Exit status for syntax errors, etc. */
58 enum { TEST_TRUE
, TEST_FALSE
, TEST_FAILURE
};
60 #if defined TEST_STANDALONE
61 # define test_exit(val) exit (val)
63 static jmp_buf test_exit_buf
;
64 static int test_error_return
= 0;
65 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
66 #endif /* !TEST_STANDALONE */
68 static int pos
; /* The offset of the current argument in ARGV. */
69 static int argc
; /* The number of arguments present in ARGV. */
70 static char **argv
; /* The argument list. */
72 static bool test_unop (char const *s
);
73 static bool unary_operator (void);
74 static bool binary_operator (bool);
75 static bool two_arguments (void);
76 static bool three_arguments (void);
77 static bool posixtest (int);
79 static bool expr (void);
80 static bool term (void);
81 static bool and (void);
82 static bool or (void);
84 static void test_syntax_error (char const *format
, char const *arg
)
86 static void beyond (void) ATTRIBUTE_NORETURN
;
89 test_syntax_error (char const *format
, char const *arg
)
91 fprintf (stderr
, "%s: ", argv
[0]);
92 fprintf (stderr
, format
, arg
);
95 test_exit (TEST_FAILURE
);
98 /* Increment our position in the argument list. Check that we're not
99 past the end of the argument list. This check is supressed if the
100 argument is false. */
107 if (f
&& pos
>= argc
)
119 * beyond - call when we're beyond the end of the argument list (an
125 test_syntax_error (_("missing argument after %s"), quote (argv
[argc
- 1]));
128 /* If the characters pointed to by STRING constitute a valid number,
129 return a pointer to the start of the number, skipping any blanks or
130 leading '+'. Otherwise, report an error and exit. */
132 find_int (char const *string
)
135 char const *number_start
;
137 for (p
= string
; isblank (to_uchar (*p
)); p
++)
155 while (isblank (to_uchar (*p
)))
161 test_syntax_error (_("invalid integer %s"), quote (string
));
164 /* Find the modification time of FILE, and stuff it into *MTIME.
165 Return true if successful. */
167 get_mtime (char const *filename
, struct timespec
*mtime
)
170 bool ok
= (stat (filename
, &finfo
) == 0);
172 static struct timespec
const zero
;
176 *mtime
= get_stat_mtime (&finfo
);
180 /* Return true if S is one of the test command's binary operators. */
182 binop (char const *s
)
184 return ((STREQ (s
, "=")) || (STREQ (s
, "!=")) || (STREQ (s
, "-nt")) ||
185 (STREQ (s
, "-ot")) || (STREQ (s
, "-ef")) || (STREQ (s
, "-eq")) ||
186 (STREQ (s
, "-ne")) || (STREQ (s
, "-lt")) || (STREQ (s
, "-le")) ||
187 (STREQ (s
, "-gt")) || (STREQ (s
, "-ge")));
191 * term - parse a term and return 1 or 0 depending on whether the term
192 * evaluates to true or false, respectively.
195 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
196 * '-'('L'|'x') filename
198 * '-'('z'|'n') string
200 * string ('!='|'=') string
201 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
202 * file '-'(nt|ot|ef) file
206 * positive and negative integers
212 bool negated
= false;
214 /* Deal with leading `not's. */
215 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
224 /* A paren-bracketed argument. */
225 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0')
232 pos
+ nargs
< argc
&& ! STREQ (argv
[pos
+ nargs
], ")");
240 value
= posixtest (nargs
);
242 test_syntax_error (_("')' expected"), NULL
);
244 if (argv
[pos
][0] != ')' || argv
[pos
][1])
245 test_syntax_error (_("')' expected, found %s"), argv
[pos
]);
249 /* Are there enough arguments left that this could be dyadic? */
250 else if (4 <= argc
- pos
&& STREQ (argv
[pos
], "-l") && binop (argv
[pos
+ 2]))
251 value
= binary_operator (true);
252 else if (3 <= argc
- pos
&& binop (argv
[pos
+ 1]))
253 value
= binary_operator (false);
255 /* It might be a switch type argument. */
256 else if (argv
[pos
][0] == '-' && argv
[pos
][1] && argv
[pos
][2] == '\0')
258 if (test_unop (argv
[pos
]))
259 value
= unary_operator ();
261 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
265 value
= (argv
[pos
][0] != '\0');
269 return negated
^ value
;
273 binary_operator (bool l_is_l
)
276 struct stat stat_buf
, stat_spare
;
277 /* Is the right integer expression of the form '-l string'? */
284 if ((op
< argc
- 2) && STREQ (argv
[op
+ 1], "-l"))
292 if (argv
[op
][0] == '-')
294 /* check for eq, nt, and stuff */
295 if ((((argv
[op
][1] == 'l' || argv
[op
][1] == 'g')
296 && (argv
[op
][2] == 'e' || argv
[op
][2] == 't'))
297 || (argv
[op
][1] == 'e' && argv
[op
][2] == 'q')
298 || (argv
[op
][1] == 'n' && argv
[op
][2] == 'e'))
301 char lbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
302 char rbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
303 char const *l
= (l_is_l
304 ? umaxtostr (strlen (argv
[op
- 1]), lbuf
)
305 : find_int (argv
[op
- 1]));
306 char const *r
= (r_is_l
307 ? umaxtostr (strlen (argv
[op
+ 2]), rbuf
)
308 : find_int (argv
[op
+ 1]));
309 int cmp
= strintcmp (l
, r
);
310 bool xe_operator
= (argv
[op
][2] == 'e');
312 return (argv
[op
][1] == 'l' ? cmp
< xe_operator
313 : argv
[op
][1] == 'g' ? cmp
> - xe_operator
314 : (cmp
!= 0) == xe_operator
);
323 if (argv
[op
][2] == 't' && !argv
[op
][3])
325 /* nt - newer than */
326 struct timespec lt
, rt
;
330 test_syntax_error (_("-nt does not accept -l"), NULL
);
331 le
= get_mtime (argv
[op
- 1], <
);
332 re
= get_mtime (argv
[op
+ 1], &rt
);
333 return le
&& (!re
|| timespec_cmp (lt
, rt
) > 0);
338 if (argv
[op
][2] == 'f' && !argv
[op
][3])
340 /* ef - hard link? */
343 test_syntax_error (_("-ef does not accept -l"), NULL
);
344 return (stat (argv
[op
- 1], &stat_buf
) == 0
345 && stat (argv
[op
+ 1], &stat_spare
) == 0
346 && stat_buf
.st_dev
== stat_spare
.st_dev
347 && stat_buf
.st_ino
== stat_spare
.st_ino
);
352 if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
354 /* ot - older than */
355 struct timespec lt
, rt
;
359 test_syntax_error (_("-ot does not accept -l"), NULL
);
360 le
= get_mtime (argv
[op
- 1], <
);
361 re
= get_mtime (argv
[op
+ 1], &rt
);
362 return re
&& (!le
|| timespec_cmp (lt
, rt
) < 0);
367 /* FIXME: is this dead code? */
368 test_syntax_error (_("unknown binary operator"), argv
[op
]);
371 if (argv
[op
][0] == '=' && !argv
[op
][1])
373 bool value
= STREQ (argv
[pos
], argv
[pos
+ 2]);
378 if (STREQ (argv
[op
], "!="))
380 bool value
= !STREQ (argv
[pos
], argv
[pos
+ 2]);
390 unary_operator (void)
392 struct stat stat_buf
;
394 switch (argv
[pos
][1])
399 /* All of the following unary operators use unary_advance (), which
400 checks to make sure that there is an argument, and then advances
401 pos right past it. This means that pos - 1 is the location of the
404 case 'a': /* file exists in the file system? */
407 return stat (argv
[pos
- 1], &stat_buf
) == 0;
409 case 'r': /* file is readable? */
411 return euidaccess (argv
[pos
- 1], R_OK
) == 0;
413 case 'w': /* File is writable? */
415 return euidaccess (argv
[pos
- 1], W_OK
) == 0;
417 case 'x': /* File is executable? */
419 return euidaccess (argv
[pos
- 1], X_OK
) == 0;
421 case 'O': /* File is owned by you? */
423 return (stat (argv
[pos
- 1], &stat_buf
) == 0
424 && (geteuid () == stat_buf
.st_uid
));
426 case 'G': /* File is owned by your group? */
428 return (stat (argv
[pos
- 1], &stat_buf
) == 0
429 && (getegid () == stat_buf
.st_gid
));
431 case 'f': /* File is a file? */
433 /* Under POSIX, -f is true if the given file exists
434 and is a regular file. */
435 return (stat (argv
[pos
- 1], &stat_buf
) == 0
436 && S_ISREG (stat_buf
.st_mode
));
438 case 'd': /* File is a directory? */
440 return (stat (argv
[pos
- 1], &stat_buf
) == 0
441 && S_ISDIR (stat_buf
.st_mode
));
443 case 's': /* File has something in it? */
445 return (stat (argv
[pos
- 1], &stat_buf
) == 0
446 && 0 < stat_buf
.st_size
);
448 case 'S': /* File is a socket? */
450 return (stat (argv
[pos
- 1], &stat_buf
) == 0
451 && S_ISSOCK (stat_buf
.st_mode
));
453 case 'c': /* File is character special? */
455 return (stat (argv
[pos
- 1], &stat_buf
) == 0
456 && S_ISCHR (stat_buf
.st_mode
));
458 case 'b': /* File is block special? */
460 return (stat (argv
[pos
- 1], &stat_buf
) == 0
461 && S_ISBLK (stat_buf
.st_mode
));
463 case 'p': /* File is a named pipe? */
465 return (stat (argv
[pos
- 1], &stat_buf
) == 0
466 && S_ISFIFO (stat_buf
.st_mode
));
468 case 'L': /* Same as -h */
471 case 'h': /* File is a symbolic link? */
473 return (lstat (argv
[pos
- 1], &stat_buf
) == 0
474 && S_ISLNK (stat_buf
.st_mode
));
476 case 'u': /* File is setuid? */
478 return (stat (argv
[pos
- 1], &stat_buf
) == 0
479 && (stat_buf
.st_mode
& S_ISUID
));
481 case 'g': /* File is setgid? */
483 return (stat (argv
[pos
- 1], &stat_buf
) == 0
484 && (stat_buf
.st_mode
& S_ISGID
));
486 case 'k': /* File has sticky bit set? */
488 return (stat (argv
[pos
- 1], &stat_buf
) == 0
489 && (stat_buf
.st_mode
& S_ISVTX
));
491 case 't': /* File (fd) is a terminal? */
496 arg
= find_int (argv
[pos
- 1]);
498 fd
= strtol (arg
, NULL
, 10);
499 return (errno
!= ERANGE
&& 0 <= fd
&& fd
<= INT_MAX
&& isatty (fd
));
502 case 'n': /* True if arg has some length. */
504 return argv
[pos
- 1][0] != 0;
506 case 'z': /* True if arg has no length. */
508 return argv
[pos
- 1][0] == '\0';
525 if (! (pos
< argc
&& STREQ (argv
[pos
], "-a")))
544 if (! (pos
< argc
&& STREQ (argv
[pos
], "-o")))
560 return or (); /* Same with this. */
563 /* Return true if OP is one of the test command's unary operators. */
565 test_unop (char const *op
)
572 case 'a': case 'b': case 'c': case 'd': case 'e':
573 case 'f': case 'g': case 'h': case 'k': case 'n':
574 case 'o': case 'p': case 'r': case 's': case 't':
575 case 'u': case 'w': case 'x': case 'z':
576 case 'G': case 'L': case 'O': case 'S': case 'N':
586 return argv
[pos
++][0] != '\0';
594 if (STREQ (argv
[pos
], "!"))
597 value
= ! one_argument ();
599 else if (argv
[pos
][0] == '-'
600 && argv
[pos
][1] != '\0'
601 && argv
[pos
][2] == '\0')
603 if (test_unop (argv
[pos
]))
604 value
= unary_operator ();
606 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
614 three_arguments (void)
618 if (binop (argv
[pos
+ 1]))
619 value
= binary_operator (false);
620 else if (STREQ (argv
[pos
], "!"))
623 value
= !two_arguments ();
625 else if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 2], ")"))
628 value
= one_argument ();
631 else if (STREQ (argv
[pos
+ 1], "-a") || STREQ (argv
[pos
+ 1], "-o"))
634 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
638 /* This is an implementation of a Posix.2 proposal by David Korn. */
640 posixtest (int nargs
)
647 value
= one_argument ();
651 value
= two_arguments ();
655 value
= three_arguments ();
659 if (STREQ (argv
[pos
], "!"))
662 value
= !three_arguments ();
665 if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 3], ")"))
668 value
= two_arguments ();
683 #if defined TEST_STANDALONE
684 # include "long-options.h"
689 if (status
!= EXIT_SUCCESS
)
690 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
695 Usage: test EXPRESSION\n\
697 or: [ EXPRESSION ]\n\
702 Exit with the status determined by EXPRESSION.\n\
705 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
706 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
709 An omitted EXPRESSION defaults to false. Otherwise,\n\
710 EXPRESSION is true or false and sets exit status. It is one of:\n\
714 ( EXPRESSION ) EXPRESSION is true\n\
715 ! EXPRESSION EXPRESSION is false\n\
716 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
717 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
721 -n STRING the length of STRING is nonzero\n\
722 STRING equivalent to -n STRING\n\
723 -z STRING the length of STRING is zero\n\
724 STRING1 = STRING2 the strings are equal\n\
725 STRING1 != STRING2 the strings are not equal\n\
729 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
730 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
731 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
732 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
733 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
734 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
738 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
739 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
740 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
744 -b FILE FILE exists and is block special\n\
745 -c FILE FILE exists and is character special\n\
746 -d FILE FILE exists and is a directory\n\
747 -e FILE FILE exists\n\
750 -f FILE FILE exists and is a regular file\n\
751 -g FILE FILE exists and is set-group-ID\n\
752 -G FILE FILE exists and is owned by the effective group ID\n\
753 -h FILE FILE exists and is a symbolic link (same as -L)\n\
754 -k FILE FILE exists and has its sticky bit set\n\
757 -L FILE FILE exists and is a symbolic link (same as -h)\n\
758 -O FILE FILE exists and is owned by the effective user ID\n\
759 -p FILE FILE exists and is a named pipe\n\
760 -r FILE FILE exists and read permission is granted\n\
761 -s FILE FILE exists and has a size greater than zero\n\
764 -S FILE FILE exists and is a socket\n\
765 -t FD file descriptor FD is opened on a terminal\n\
766 -u FILE FILE exists and its set-user-ID bit is set\n\
767 -w FILE FILE exists and write permission is granted\n\
768 -x FILE FILE exists and execute (or search) permission is granted\n\
772 Except for -h and -L, all FILE-related tests dereference symbolic links.\n\
773 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
774 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
776 printf (USAGE_BUILTIN_WARNING
, _("test and/or ["));
777 emit_bug_reporting_address ();
781 #endif /* TEST_STANDALONE */
783 #if !defined TEST_STANDALONE
784 # define main test_command
787 #define AUTHORS "Kevin Braunsdorf", "Matthew Bradburn"
796 main (int margc
, char **margv
)
800 #if !defined TEST_STANDALONE
803 code
= setjmp (test_exit_buf
);
806 return (test_error_return
);
807 #else /* TEST_STANDALONE */
808 initialize_main (&margc
, &margv
);
809 program_name
= margv
[0];
810 setlocale (LC_ALL
, "");
811 bindtextdomain (PACKAGE
, LOCALEDIR
);
812 textdomain (PACKAGE
);
814 initialize_exit_failure (TEST_FAILURE
);
815 atexit (close_stdout
);
816 #endif /* TEST_STANDALONE */
822 /* Recognize --help or --version, but only when invoked in the
823 "[" form, and when the last argument is not "]". POSIX
824 allows "[ --help" and "[ --version" to have the usual GNU
825 behavior, but it requires "test --help" and "test --version"
826 to exit silently with status 0. */
827 if (margc
< 2 || !STREQ (margv
[margc
- 1], "]"))
829 parse_long_options (margc
, margv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
830 usage
, AUTHORS
, (char const *) NULL
);
831 test_syntax_error (_("missing `]'"), NULL
);
841 test_exit (TEST_FALSE
);
843 value
= posixtest (argc
- 1);
846 test_syntax_error (_("extra argument %s"), quote (argv
[pos
]));
848 test_exit (value
? TEST_TRUE
: TEST_FALSE
);