1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell by bfox. */
5 /* Copyright (C) 1987-2005, 2007-2011 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Define TEST_STANDALONE to get the /bin/test version. Otherwise, you get
21 the shell builtin version. */
25 #include <sys/types.h>
27 #define TEST_STANDALONE 1
33 /* The official name of this program (e.g., no `g' prefix). */
35 # define PROGRAM_NAME "["
37 # define PROGRAM_NAME "test"
42 #include "stat-time.h"
43 #include "strnumcmp.h"
46 # include <sys/param.h>
49 /* Exit status for syntax errors, etc. */
50 enum { TEST_TRUE
, TEST_FALSE
, TEST_FAILURE
};
52 #if defined TEST_STANDALONE
53 # define test_exit(val) exit (val)
55 static jmp_buf test_exit_buf
;
56 static int test_error_return
= 0;
57 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
58 #endif /* !TEST_STANDALONE */
60 static int pos
; /* The offset of the current argument in ARGV. */
61 static int argc
; /* The number of arguments present in ARGV. */
62 static char **argv
; /* The argument list. */
64 static bool test_unop (char const *s
);
65 static bool unary_operator (void);
66 static bool binary_operator (bool);
67 static bool two_arguments (void);
68 static bool three_arguments (void);
69 static bool posixtest (int);
71 static bool expr (void);
72 static bool term (void);
73 static bool and (void);
74 static bool or (void);
76 static void test_syntax_error (char const *format
, char const *arg
)
78 static void beyond (void) ATTRIBUTE_NORETURN
;
81 test_syntax_error (char const *format
, char const *arg
)
83 fprintf (stderr
, "%s: ", argv
[0]);
84 fprintf (stderr
, format
, arg
);
87 test_exit (TEST_FAILURE
);
90 /* Increment our position in the argument list. Check that we're not
91 past the end of the argument list. This check is supressed if the
111 * beyond - call when we're beyond the end of the argument list (an
117 test_syntax_error (_("missing argument after %s"), quote (argv
[argc
- 1]));
120 /* If the characters pointed to by STRING constitute a valid number,
121 return a pointer to the start of the number, skipping any blanks or
122 leading '+'. Otherwise, report an error and exit. */
124 find_int (char const *string
)
127 char const *number_start
;
129 for (p
= string
; isblank (to_uchar (*p
)); p
++)
147 while (isblank (to_uchar (*p
)))
153 test_syntax_error (_("invalid integer %s"), quote (string
));
156 /* Find the modification time of FILE, and stuff it into *MTIME.
157 Return true if successful. */
159 get_mtime (char const *filename
, struct timespec
*mtime
)
162 bool ok
= (stat (filename
, &finfo
) == 0);
164 static struct timespec
const zero
;
168 *mtime
= get_stat_mtime (&finfo
);
172 /* Return true if S is one of the test command's binary operators. */
174 binop (char const *s
)
176 return ((STREQ (s
, "=")) || (STREQ (s
, "!=")) || (STREQ (s
, "==")) ||
177 (STREQ (s
, "-nt")) ||
178 (STREQ (s
, "-ot")) || (STREQ (s
, "-ef")) || (STREQ (s
, "-eq")) ||
179 (STREQ (s
, "-ne")) || (STREQ (s
, "-lt")) || (STREQ (s
, "-le")) ||
180 (STREQ (s
, "-gt")) || (STREQ (s
, "-ge")));
184 * term - parse a term and return 1 or 0 depending on whether the term
185 * evaluates to true or false, respectively.
188 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
189 * '-'('L'|'x') filename
191 * '-'('z'|'n') string
193 * string ('!='|'=') string
194 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
195 * file '-'(nt|ot|ef) file
199 * positive and negative integers
205 bool negated
= false;
207 /* Deal with leading `not's. */
208 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
217 /* A paren-bracketed argument. */
218 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0')
225 pos
+ nargs
< argc
&& ! STREQ (argv
[pos
+ nargs
], ")");
233 value
= posixtest (nargs
);
235 test_syntax_error (_("')' expected"), NULL
);
237 if (argv
[pos
][0] != ')' || argv
[pos
][1])
238 test_syntax_error (_("')' expected, found %s"), argv
[pos
]);
242 /* Are there enough arguments left that this could be dyadic? */
243 else if (4 <= argc
- pos
&& STREQ (argv
[pos
], "-l") && binop (argv
[pos
+ 2]))
244 value
= binary_operator (true);
245 else if (3 <= argc
- pos
&& binop (argv
[pos
+ 1]))
246 value
= binary_operator (false);
248 /* It might be a switch type argument. */
249 else if (argv
[pos
][0] == '-' && argv
[pos
][1] && argv
[pos
][2] == '\0')
251 if (test_unop (argv
[pos
]))
252 value
= unary_operator ();
254 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
258 value
= (argv
[pos
][0] != '\0');
262 return negated
^ value
;
266 binary_operator (bool l_is_l
)
269 struct stat stat_buf
, stat_spare
;
270 /* Is the right integer expression of the form '-l string'? */
277 if ((op
< argc
- 2) && STREQ (argv
[op
+ 1], "-l"))
285 if (argv
[op
][0] == '-')
287 /* check for eq, nt, and stuff */
288 if ((((argv
[op
][1] == 'l' || argv
[op
][1] == 'g')
289 && (argv
[op
][2] == 'e' || argv
[op
][2] == 't'))
290 || (argv
[op
][1] == 'e' && argv
[op
][2] == 'q')
291 || (argv
[op
][1] == 'n' && argv
[op
][2] == 'e'))
294 char lbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
295 char rbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
296 char const *l
= (l_is_l
297 ? umaxtostr (strlen (argv
[op
- 1]), lbuf
)
298 : find_int (argv
[op
- 1]));
299 char const *r
= (r_is_l
300 ? umaxtostr (strlen (argv
[op
+ 2]), rbuf
)
301 : find_int (argv
[op
+ 1]));
302 int cmp
= strintcmp (l
, r
);
303 bool xe_operator
= (argv
[op
][2] == 'e');
305 return (argv
[op
][1] == 'l' ? cmp
< xe_operator
306 : argv
[op
][1] == 'g' ? cmp
> - xe_operator
307 : (cmp
!= 0) == xe_operator
);
316 if (argv
[op
][2] == 't' && !argv
[op
][3])
318 /* nt - newer than */
319 struct timespec lt
, rt
;
322 if (l_is_l
|| r_is_l
)
323 test_syntax_error (_("-nt does not accept -l"), NULL
);
324 le
= get_mtime (argv
[op
- 1], <
);
325 re
= get_mtime (argv
[op
+ 1], &rt
);
326 return le
&& (!re
|| timespec_cmp (lt
, rt
) > 0);
331 if (argv
[op
][2] == 'f' && !argv
[op
][3])
333 /* ef - hard link? */
335 if (l_is_l
|| r_is_l
)
336 test_syntax_error (_("-ef does not accept -l"), NULL
);
337 return (stat (argv
[op
- 1], &stat_buf
) == 0
338 && stat (argv
[op
+ 1], &stat_spare
) == 0
339 && stat_buf
.st_dev
== stat_spare
.st_dev
340 && stat_buf
.st_ino
== stat_spare
.st_ino
);
345 if ('t' == argv
[op
][2] && '\000' == argv
[op
][3])
347 /* ot - older than */
348 struct timespec lt
, rt
;
351 if (l_is_l
|| r_is_l
)
352 test_syntax_error (_("-ot does not accept -l"), NULL
);
353 le
= get_mtime (argv
[op
- 1], <
);
354 re
= get_mtime (argv
[op
+ 1], &rt
);
355 return re
&& (!le
|| timespec_cmp (lt
, rt
) < 0);
360 /* FIXME: is this dead code? */
361 test_syntax_error (_("unknown binary operator"), argv
[op
]);
364 if (argv
[op
][0] == '=' && (!argv
[op
][1] ||
365 ((argv
[op
][1] == '=') && !argv
[op
][2])))
367 bool value
= STREQ (argv
[pos
], argv
[pos
+ 2]);
372 if (STREQ (argv
[op
], "!="))
374 bool value
= !STREQ (argv
[pos
], argv
[pos
+ 2]);
384 unary_operator (void)
386 struct stat stat_buf
;
388 switch (argv
[pos
][1])
393 /* All of the following unary operators use unary_advance (), which
394 checks to make sure that there is an argument, and then advances
395 pos right past it. This means that pos - 1 is the location of the
398 case 'a': /* file exists in the file system? */
401 return stat (argv
[pos
- 1], &stat_buf
) == 0;
403 case 'r': /* file is readable? */
405 return euidaccess (argv
[pos
- 1], R_OK
) == 0;
407 case 'w': /* File is writable? */
409 return euidaccess (argv
[pos
- 1], W_OK
) == 0;
411 case 'x': /* File is executable? */
413 return euidaccess (argv
[pos
- 1], X_OK
) == 0;
415 case 'O': /* File is owned by you? */
418 if (stat (argv
[pos
- 1], &stat_buf
) != 0)
421 uid_t euid
= geteuid ();
423 return ! (euid
== NO_UID
&& errno
) && euid
== stat_buf
.st_uid
;
426 case 'G': /* File is owned by your group? */
429 if (stat (argv
[pos
- 1], &stat_buf
) != 0)
432 gid_t egid
= getegid ();
434 return ! (egid
== NO_GID
&& errno
) && egid
== stat_buf
.st_gid
;
437 case 'f': /* File is a file? */
439 /* Under POSIX, -f is true if the given file exists
440 and is a regular file. */
441 return (stat (argv
[pos
- 1], &stat_buf
) == 0
442 && S_ISREG (stat_buf
.st_mode
));
444 case 'd': /* File is a directory? */
446 return (stat (argv
[pos
- 1], &stat_buf
) == 0
447 && S_ISDIR (stat_buf
.st_mode
));
449 case 's': /* File has something in it? */
451 return (stat (argv
[pos
- 1], &stat_buf
) == 0
452 && 0 < stat_buf
.st_size
);
454 case 'S': /* File is a socket? */
456 return (stat (argv
[pos
- 1], &stat_buf
) == 0
457 && S_ISSOCK (stat_buf
.st_mode
));
459 case 'c': /* File is character special? */
461 return (stat (argv
[pos
- 1], &stat_buf
) == 0
462 && S_ISCHR (stat_buf
.st_mode
));
464 case 'b': /* File is block special? */
466 return (stat (argv
[pos
- 1], &stat_buf
) == 0
467 && S_ISBLK (stat_buf
.st_mode
));
469 case 'p': /* File is a named pipe? */
471 return (stat (argv
[pos
- 1], &stat_buf
) == 0
472 && S_ISFIFO (stat_buf
.st_mode
));
474 case 'L': /* Same as -h */
477 case 'h': /* File is a symbolic link? */
479 return (lstat (argv
[pos
- 1], &stat_buf
) == 0
480 && S_ISLNK (stat_buf
.st_mode
));
482 case 'u': /* File is setuid? */
484 return (stat (argv
[pos
- 1], &stat_buf
) == 0
485 && (stat_buf
.st_mode
& S_ISUID
));
487 case 'g': /* File is setgid? */
489 return (stat (argv
[pos
- 1], &stat_buf
) == 0
490 && (stat_buf
.st_mode
& S_ISGID
));
492 case 'k': /* File has sticky bit set? */
494 return (stat (argv
[pos
- 1], &stat_buf
) == 0
495 && (stat_buf
.st_mode
& S_ISVTX
));
497 case 't': /* File (fd) is a terminal? */
502 arg
= find_int (argv
[pos
- 1]);
504 fd
= strtol (arg
, NULL
, 10);
505 return (errno
!= ERANGE
&& 0 <= fd
&& fd
<= INT_MAX
&& isatty (fd
));
508 case 'n': /* True if arg has some length. */
510 return argv
[pos
- 1][0] != 0;
512 case 'z': /* True if arg has no length. */
514 return argv
[pos
- 1][0] == '\0';
531 if (! (pos
< argc
&& STREQ (argv
[pos
], "-a")))
550 if (! (pos
< argc
&& STREQ (argv
[pos
], "-o")))
566 return or (); /* Same with this. */
569 /* Return true if OP is one of the test command's unary operators. */
571 test_unop (char const *op
)
578 case 'a': case 'b': case 'c': case 'd': case 'e':
579 case 'f': case 'g': case 'h': case 'k': case 'n':
580 case 'o': case 'p': case 'r': case 's': case 't':
581 case 'u': case 'w': case 'x': case 'z':
582 case 'G': case 'L': case 'O': case 'S': case 'N':
592 return argv
[pos
++][0] != '\0';
600 if (STREQ (argv
[pos
], "!"))
603 value
= ! one_argument ();
605 else if (argv
[pos
][0] == '-'
606 && argv
[pos
][1] != '\0'
607 && argv
[pos
][2] == '\0')
609 if (test_unop (argv
[pos
]))
610 value
= unary_operator ();
612 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
620 three_arguments (void)
624 if (binop (argv
[pos
+ 1]))
625 value
= binary_operator (false);
626 else if (STREQ (argv
[pos
], "!"))
629 value
= !two_arguments ();
631 else if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 2], ")"))
634 value
= one_argument ();
637 else if (STREQ (argv
[pos
+ 1], "-a") || STREQ (argv
[pos
+ 1], "-o"))
640 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
644 /* This is an implementation of a Posix.2 proposal by David Korn. */
646 posixtest (int nargs
)
653 value
= one_argument ();
657 value
= two_arguments ();
661 value
= three_arguments ();
665 if (STREQ (argv
[pos
], "!"))
668 value
= !three_arguments ();
671 if (STREQ (argv
[pos
], "(") && STREQ (argv
[pos
+ 3], ")"))
674 value
= two_arguments ();
689 #if defined TEST_STANDALONE
694 if (status
!= EXIT_SUCCESS
)
695 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
700 Usage: test EXPRESSION\n\
702 or: [ EXPRESSION ]\n\
707 Exit with the status determined by EXPRESSION.\n\
710 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
711 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
714 An omitted EXPRESSION defaults to false. Otherwise,\n\
715 EXPRESSION is true or false and sets exit status. It is one of:\n\
719 ( EXPRESSION ) EXPRESSION is true\n\
720 ! EXPRESSION EXPRESSION is false\n\
721 EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true\n\
722 EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true\n\
726 -n STRING the length of STRING is nonzero\n\
727 STRING equivalent to -n STRING\n\
728 -z STRING the length of STRING is zero\n\
729 STRING1 = STRING2 the strings are equal\n\
730 STRING1 != STRING2 the strings are not equal\n\
734 INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2\n\
735 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2\n\
736 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2\n\
737 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2\n\
738 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2\n\
739 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2\n\
743 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers\n\
744 FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2\n\
745 FILE1 -ot FILE2 FILE1 is older than FILE2\n\
749 -b FILE FILE exists and is block special\n\
750 -c FILE FILE exists and is character special\n\
751 -d FILE FILE exists and is a directory\n\
752 -e FILE FILE exists\n\
755 -f FILE FILE exists and is a regular file\n\
756 -g FILE FILE exists and is set-group-ID\n\
757 -G FILE FILE exists and is owned by the effective group ID\n\
758 -h FILE FILE exists and is a symbolic link (same as -L)\n\
759 -k FILE FILE exists and has its sticky bit set\n\
762 -L FILE FILE exists and is a symbolic link (same as -h)\n\
763 -O FILE FILE exists and is owned by the effective user ID\n\
764 -p FILE FILE exists and is a named pipe\n\
765 -r FILE FILE exists and read permission is granted\n\
766 -s FILE FILE exists and has a size greater than zero\n\
769 -S FILE FILE exists and is a socket\n\
770 -t FD file descriptor FD is opened on a terminal\n\
771 -u FILE FILE exists and its set-user-ID bit is set\n\
772 -w FILE FILE exists and write permission is granted\n\
773 -x FILE FILE exists and execute (or search) permission is granted\n\
777 Except for -h and -L, all FILE-related tests dereference symbolic links.\n\
778 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
779 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
783 NOTE: [ honors the --help and --version options, but test does not.\n\
784 test treats each of those as it treats any other nonempty STRING.\n\
786 printf (USAGE_BUILTIN_WARNING
, _("test and/or ["));
787 emit_ancillary_info ();
791 #endif /* TEST_STANDALONE */
793 #if !defined TEST_STANDALONE
794 # define main test_command
798 proper_name ("Kevin Braunsdorf"), \
799 proper_name ("Matthew Bradburn")
808 main (int margc
, char **margv
)
812 #if !defined TEST_STANDALONE
815 code
= setjmp (test_exit_buf
);
818 return (test_error_return
);
819 #else /* TEST_STANDALONE */
820 initialize_main (&margc
, &margv
);
821 set_program_name (margv
[0]);
822 setlocale (LC_ALL
, "");
823 bindtextdomain (PACKAGE
, LOCALEDIR
);
824 textdomain (PACKAGE
);
826 initialize_exit_failure (TEST_FAILURE
);
827 atexit (close_stdout
);
828 #endif /* TEST_STANDALONE */
834 /* Recognize --help or --version, but only when invoked in the
835 "[" form, when the last argument is not "]". Use direct
836 parsing, rather than parse_long_options, to avoid accepting
837 abbreviations. POSIX allows "[ --help" and "[ --version" to
838 have the usual GNU behavior, but it requires "test --help"
839 and "test --version" to exit silently with status 0. */
842 if (STREQ (margv
[1], "--help"))
843 usage (EXIT_SUCCESS
);
845 if (STREQ (margv
[1], "--version"))
847 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
849 test_exit (EXIT_SUCCESS
);
852 if (margc
< 2 || !STREQ (margv
[margc
- 1], "]"))
853 test_syntax_error (_("missing `]'"), NULL
);
862 test_exit (TEST_FALSE
);
864 value
= posixtest (argc
- 1);
867 test_syntax_error (_("extra argument %s"), quote (argv
[pos
]));
869 test_exit (value
? TEST_TRUE
: TEST_FALSE
);