(src_to_dest_lookup): New function.
[coreutils.git] / src / test.c
blobef2b35f960b2a8169382727ac354777078fa8351
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
12 version.
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
17 for more details.
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. */
26 #include <config.h>
27 #include <stdio.h>
28 #include <sys/types.h>
30 #define TEST_STANDALONE 1
32 #ifndef LBRACKET
33 # define LBRACKET 0
34 #endif
36 /* The official name of this program (e.g., no `g' prefix). */
37 #if LBRACKET
38 # define PROGRAM_NAME "["
39 #else
40 # define PROGRAM_NAME "test"
41 #endif
43 #include "system.h"
44 #include "error.h"
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')
54 char *program_name;
56 #if !defined (_POSIX_VERSION)
57 # include <sys/file.h>
58 #endif /* !_POSIX_VERSION */
60 extern gid_t getegid ();
61 extern uid_t geteuid ();
63 #if !defined (R_OK)
64 # define R_OK 4
65 # define W_OK 2
66 # define X_OK 1
67 # define F_OK 0
68 #endif /* R_OK */
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). */
77 #define TRUE 1
78 #define FALSE 0
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)
88 #else
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)
112 ATTRIBUTE_NORETURN;
113 static void beyond (void) ATTRIBUTE_NORETURN;
115 static void
116 test_syntax_error (char const *format, char const *arg)
118 fprintf (stderr, "%s: ", argv[0]);
119 fprintf (stderr, format, arg);
120 fflush (stderr);
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. */
127 static int
128 eaccess (char const *file, int mode)
130 static int have_ids;
131 static uid_t uid, euid;
132 static gid_t gid, egid;
133 int result;
135 if (have_ids == 0)
137 have_ids = 1;
138 uid = getuid ();
139 gid = getgid ();
140 euid = geteuid ();
141 egid = getegid ();
144 /* Set the real user and group IDs to the effective ones. */
145 if (uid != euid)
146 setreuid (euid, uid);
147 if (gid != egid)
148 setregid (egid, gid);
150 result = access (file, mode);
152 /* Restore them. */
153 if (uid != euid)
154 setreuid (uid, euid);
155 if (gid != egid)
156 setregid (gid, egid);
158 return result;
160 #else
161 # define eaccess(F, M) euidaccess (F, M)
162 #endif
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. */
167 #define advance(f) \
168 do \
170 ++pos; \
171 if ((f) && pos >= argc) \
172 beyond (); \
174 while (0)
176 #if !defined (advance)
177 static int
178 advance (int f)
180 ++pos;
182 if (f && pos >= argc)
183 beyond ();
185 #endif /* advance */
187 #define unary_advance() \
188 do \
190 advance (1); \
191 ++pos; \
193 while (0)
196 * beyond - call when we're beyond the end of the argument list (an
197 * error condition)
199 static void
200 beyond (void)
202 test_syntax_error (_("argument expected\n"), NULL);
205 /* Syntax error for when an integer argument was expected, but
206 something else was found. */
207 static void
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
215 not null. */
216 static int
217 isint (register char *string, intmax_t *result)
219 int sign;
220 intmax_t value;
222 sign = 1;
223 value = 0;
225 if (result)
226 *result = 0;
228 /* Skip leading whitespace characters. */
229 while (whitespace (*string))
230 string++;
232 if (!*string)
233 return (0);
235 /* We allow leading `-' or `+'. */
236 if (*string == '-' || *string == '+')
238 if (!digit (string[1]))
239 return (0);
241 if (*string == '-')
242 sign = -1;
244 string++;
247 while (digit (*string))
249 if (result)
250 value = (value * 10) + digit_value (*string);
251 string++;
254 /* Skip trailing whitespace, if any. */
255 while (whitespace (*string))
256 string++;
258 /* Error if not at end of string. */
259 if (*string)
260 return (0);
262 if (result)
264 value *= sign;
265 *result = value;
268 return (1);
271 /* Find the modification time of FILE, and stuff it into *AGE.
272 Return 0 if successful, -1 if not. */
273 static int
274 age_of (char *filename, time_t *age)
276 struct stat finfo;
277 int r = stat (filename, &finfo);
278 if (r == 0)
279 *age = finfo.st_mtime;
280 return r;
284 * term - parse a term and return 1 or 0 depending on whether the term
285 * evaluates to true or false, respectively.
287 * term ::=
288 * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
289 * '-'('L'|'x') filename
290 * '-t' [ int ]
291 * '-'('z'|'n') string
292 * string
293 * string ('!='|'=') string
294 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
295 * file '-'(nt|ot|ef) file
296 * '(' <expr> ')'
297 * int ::=
298 * '-l' string
299 * positive and negative integers
301 static int
302 term (void)
304 int value;
306 if (pos >= argc)
307 beyond ();
309 /* Deal with leading `not's. */
310 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
312 value = 0;
313 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
315 advance (1);
316 value = 1 - value;
319 return (value ? !term() : term());
322 /* A paren-bracketed argument. */
323 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
325 int nargs;
327 advance (1);
329 for (nargs = 1;
330 pos + nargs < argc && ! STREQ (argv[pos + nargs], ")");
331 nargs++)
332 if (nargs == 4)
334 nargs = argc - pos;
335 break;
338 value = posixtest (nargs);
339 if (argv[pos] == 0)
340 test_syntax_error (_("')' expected\n"), NULL);
341 else
342 if (argv[pos][0] != ')' || argv[pos][1])
343 test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
344 advance (0);
345 return (value);
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 ();
359 else
360 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
362 else
364 value = (argv[pos][0] != '\0');
365 advance (0);
368 return (value);
371 static int
372 binary_operator (bool l_is_l)
374 register int op;
375 struct stat stat_buf, stat_spare;
376 intmax_t l, r;
377 int value;
378 /* Is the right integer expression of the form '-l string'? */
379 int r_is_l;
381 if (l_is_l)
382 advance (0);
383 op = pos + 1;
385 if ((op < argc - 2) && (strcmp (argv[op + 1], "-l") == 0))
387 r_is_l = 1;
388 advance (0);
390 else
391 r_is_l = 0;
393 if (argv[op][0] == '-')
395 /* check for eq, nt, and stuff */
396 switch (argv[op][1])
398 default:
399 break;
401 case 'l':
402 if (argv[op][2] == 't' && !argv[op][3])
404 /* lt */
405 if (l_is_l)
406 l = strlen (argv[op - 1]);
407 else
409 if (!isint (argv[op - 1], &l))
410 integer_expected_error (_("before -lt"));
413 if (r_is_l)
414 r = strlen (argv[op + 2]);
415 else
417 if (!isint (argv[op + 1], &r))
418 integer_expected_error (_("after -lt"));
420 pos += 3;
421 return (TRUE == (l < r));
424 if (argv[op][2] == 'e' && !argv[op][3])
426 /* le */
427 if (l_is_l)
428 l = strlen (argv[op - 1]);
429 else
431 if (!isint (argv[op - 1], &l))
432 integer_expected_error (_("before -le"));
434 if (r_is_l)
435 r = strlen (argv[op + 2]);
436 else
438 if (!isint (argv[op + 1], &r))
439 integer_expected_error (_("after -le"));
441 pos += 3;
442 return (TRUE == (l <= r));
444 break;
446 case 'g':
447 if (argv[op][2] == 't' && !argv[op][3])
449 /* gt integer greater than */
450 if (l_is_l)
451 l = strlen (argv[op - 1]);
452 else
454 if (!isint (argv[op - 1], &l))
455 integer_expected_error (_("before -gt"));
457 if (r_is_l)
458 r = strlen (argv[op + 2]);
459 else
461 if (!isint (argv[op + 1], &r))
462 integer_expected_error (_("after -gt"));
464 pos += 3;
465 return (TRUE == (l > r));
468 if (argv[op][2] == 'e' && !argv[op][3])
470 /* ge - integer greater than or equal to */
471 if (l_is_l)
472 l = strlen (argv[op - 1]);
473 else
475 if (!isint (argv[op - 1], &l))
476 integer_expected_error (_("before -ge"));
478 if (r_is_l)
479 r = strlen (argv[op + 2]);
480 else
482 if (!isint (argv[op + 1], &r))
483 integer_expected_error (_("after -ge"));
485 pos += 3;
486 return (TRUE == (l >= r));
488 break;
490 case 'n':
491 if (argv[op][2] == 't' && !argv[op][3])
493 /* nt - newer than */
494 time_t lt, rt;
495 int le, re;
496 pos += 3;
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], &lt);
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 */
507 if (l_is_l)
508 l = strlen (argv[op - 1]);
509 else
511 if (!isint (argv[op - 1], &l))
512 integer_expected_error (_("before -ne"));
514 if (r_is_l)
515 r = strlen (argv[op + 2]);
516 else
518 if (!isint (argv[op + 1], &r))
519 integer_expected_error (_("after -ne"));
521 pos += 3;
522 return (TRUE == (l != r));
524 break;
526 case 'e':
527 if (argv[op][2] == 'q' && !argv[op][3])
529 /* eq - integer equal */
530 if (l_is_l)
531 l = strlen (argv[op - 1]);
532 else
534 if (!isint (argv[op - 1], &l))
535 integer_expected_error (_("before -eq"));
537 if (r_is_l)
538 r = strlen (argv[op + 2]);
539 else
541 if (!isint (argv[op + 1], &r))
542 integer_expected_error (_("after -eq"));
544 pos += 3;
545 return (TRUE == (l == r));
548 if (argv[op][2] == 'f' && !argv[op][3])
550 /* ef - hard link? */
551 pos += 3;
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)
555 return (FALSE);
556 if (stat (argv[op + 1], &stat_spare) < 0)
557 return (FALSE);
558 return (TRUE ==
559 (stat_buf.st_dev == stat_spare.st_dev &&
560 stat_buf.st_ino == stat_spare.st_ino));
562 break;
564 case 'o':
565 if ('t' == argv[op][2] && '\000' == argv[op][3])
567 /* ot - older than */
568 time_t lt, rt;
569 int le, re;
570 pos += 3;
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], &lt);
574 re = age_of (argv[op + 1], &rt);
575 return le < re || (re == 0 && lt < rt);
577 break;
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);
587 pos += 3;
588 return (TRUE == value);
591 if (strcmp (argv[op], "!=") == 0)
593 value = (strcmp (argv[pos], argv[pos + 2]) != 0);
594 pos += 3;
595 return (TRUE == value);
598 /* Not reached. */
599 abort ();
602 static int
603 unary_operator (void)
605 int value;
606 struct stat stat_buf;
608 switch (argv[pos][1])
610 default:
611 return (FALSE);
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
616 argument. */
618 case 'a': /* file exists in the file system? */
619 case 'e':
620 unary_advance ();
621 value = -1 != stat (argv[pos - 1], &stat_buf);
622 return (TRUE == value);
624 case 'r': /* file is readable? */
625 unary_advance ();
626 value = -1 != eaccess (argv[pos - 1], R_OK);
627 return (TRUE == value);
629 case 'w': /* File is writable? */
630 unary_advance ();
631 value = -1 != eaccess (argv[pos - 1], W_OK);
632 return (TRUE == value);
634 case 'x': /* File is executable? */
635 unary_advance ();
636 value = -1 != eaccess (argv[pos - 1], X_OK);
637 return (TRUE == value);
639 case 'O': /* File is owned by you? */
640 unary_advance ();
641 if (stat (argv[pos - 1], &stat_buf) < 0)
642 return (FALSE);
644 return (TRUE == (geteuid () == stat_buf.st_uid));
646 case 'G': /* File is owned by your group? */
647 unary_advance ();
648 if (stat (argv[pos - 1], &stat_buf) < 0)
649 return (FALSE);
651 return (TRUE == (getegid () == stat_buf.st_gid));
653 case 'f': /* File is a file? */
654 unary_advance ();
655 if (stat (argv[pos - 1], &stat_buf) < 0)
656 return (FALSE);
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? */
664 unary_advance ();
665 if (stat (argv[pos - 1], &stat_buf) < 0)
666 return (FALSE);
668 return (TRUE == (S_ISDIR (stat_buf.st_mode)));
670 case 's': /* File has something in it? */
671 unary_advance ();
672 if (stat (argv[pos - 1], &stat_buf) < 0)
673 return (FALSE);
675 return (TRUE == (stat_buf.st_size > (off_t) 0));
677 case 'S': /* File is a socket? */
678 #if !defined (S_ISSOCK)
679 return (FALSE);
680 #else
681 unary_advance ();
683 if (stat (argv[pos - 1], &stat_buf) < 0)
684 return (FALSE);
686 return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
687 #endif /* S_ISSOCK */
689 case 'c': /* File is character special? */
690 unary_advance ();
691 if (stat (argv[pos - 1], &stat_buf) < 0)
692 return (FALSE);
694 return (TRUE == (S_ISCHR (stat_buf.st_mode)));
696 case 'b': /* File is block special? */
697 unary_advance ();
698 if (stat (argv[pos - 1], &stat_buf) < 0)
699 return (FALSE);
701 return (TRUE == (S_ISBLK (stat_buf.st_mode)));
703 case 'p': /* File is a named pipe? */
704 unary_advance ();
705 #ifndef S_ISFIFO
706 return (FALSE);
707 #else
708 if (stat (argv[pos - 1], &stat_buf) < 0)
709 return (FALSE);
710 return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
711 #endif /* S_ISFIFO */
713 case 'L': /* Same as -h */
714 /*FALLTHROUGH*/
716 case 'h': /* File is a symbolic link? */
717 unary_advance ();
718 #ifndef S_ISLNK
719 return (FALSE);
720 #else
721 /* An empty filename is not a valid pathname. */
722 if ((argv[pos - 1][0] == '\0') ||
723 (lstat (argv[pos - 1], &stat_buf) < 0))
724 return (FALSE);
726 return (TRUE == (S_ISLNK (stat_buf.st_mode)));
727 #endif /* S_IFLNK */
729 case 'u': /* File is setuid? */
730 unary_advance ();
731 #ifndef S_ISUID
732 return (FALSE);
733 #else
734 if (stat (argv[pos - 1], &stat_buf) < 0)
735 return (FALSE);
737 return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
738 #endif
740 case 'g': /* File is setgid? */
741 unary_advance ();
742 #ifndef S_ISGID
743 return (FALSE);
744 #else
745 if (stat (argv[pos - 1], &stat_buf) < 0)
746 return (FALSE);
748 return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
749 #endif
751 case 'k': /* File has sticky bit set? */
752 unary_advance ();
753 if (stat (argv[pos - 1], &stat_buf) < 0)
754 return (FALSE);
755 #ifndef S_ISVTX
756 /* This is not Posix, and is not defined on some Posix systems. */
757 return (FALSE);
758 #else
759 return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
760 #endif
762 case 't': /* File (fd) is a terminal? */
764 intmax_t fd;
765 unary_advance ();
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. */
772 unary_advance ();
773 return (TRUE == (argv[pos - 1][0] != 0));
775 case 'z': /* True if arg has no length. */
776 unary_advance ();
777 return (TRUE == (argv[pos - 1][0] == '\0'));
782 * and:
783 * term
784 * term '-a' and
786 static int
787 and (void)
789 int value;
791 value = term ();
792 while ((pos < argc) && strcmp (argv[pos], "-a") == 0)
794 advance (0);
795 value = TRUTH_AND (value, and ());
797 return (TRUE == value);
801 * or:
802 * and
803 * and '-o' or
805 static int
806 or (void)
808 int value;
810 value = and ();
812 while ((pos < argc) && strcmp (argv[pos], "-o") == 0)
814 advance (0);
815 value = TRUTH_OR (value, or ());
818 return (TRUE == value);
822 * expr:
823 * or
825 static int
826 expr (void)
828 if (pos >= argc)
829 beyond ();
831 return (FALSE ^ (or ())); /* Same with this. */
834 /* Return TRUE if S is one of the test command's binary operators. */
835 static int
836 binop (char *s)
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. */
845 static int
846 test_unop (char const *op)
848 if (op[0] != '-')
849 return (0);
851 switch (op[1])
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':
858 return (1);
861 return (0);
864 static int
865 one_argument (void)
867 return argv[pos++][0] != '\0';
870 static int
871 two_arguments (void)
873 int value;
875 if (STREQ (argv[pos], "!"))
877 advance (0);
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 ();
886 else
887 test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
889 else
890 beyond ();
891 return (value);
894 static int
895 three_arguments (void)
897 int value;
899 if (binop (argv[pos + 1]))
900 value = binary_operator (false);
901 else if (STREQ (argv[pos], "!"))
903 advance (1);
904 value = !two_arguments ();
906 else if (STREQ (argv[pos], "(") && STREQ (argv[pos + 2], ")"))
908 advance (0);
909 value = one_argument ();
910 advance (0);
912 else if (STREQ (argv[pos + 1], "-a") || STREQ (argv[pos + 1], "-o"))
913 value = expr ();
914 else
915 test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
916 return (value);
919 /* This is an implementation of a Posix.2 proposal by David Korn. */
920 static int
921 posixtest (int nargs)
923 int value;
925 switch (nargs)
927 case 1:
928 value = one_argument ();
929 break;
931 case 2:
932 value = two_arguments ();
933 break;
935 case 3:
936 value = three_arguments ();
937 break;
939 case 4:
940 if (STREQ (argv[pos], "!"))
942 advance (1);
943 value = !three_arguments ();
944 break;
946 if (STREQ (argv[pos], "(") && STREQ (argv[pos + 3], ")"))
948 advance (0);
949 value = two_arguments ();
950 advance (0);
951 break;
953 /* FALLTHROUGH */
954 case 5:
955 default:
956 if (nargs <= 0)
957 abort ();
958 value = expr ();
961 return (value);
964 #if defined (TEST_STANDALONE)
965 # include "long-options.h"
967 void
968 usage (int status)
970 if (status != EXIT_SUCCESS)
971 fprintf (stderr, _("Try `%s --help' for more information.\n"),
972 program_name);
973 else
975 fputs (_("\
976 Usage: test EXPRESSION\n\
977 or: [ EXPRESSION ]\n\
978 or: [ OPTION\n\
979 Exit with the status determined by EXPRESSION.\n\
981 "), stdout);
982 fputs (HELP_OPTION_DESCRIPTION, stdout);
983 fputs (VERSION_OPTION_DESCRIPTION, stdout);
984 fputs (_("\
986 EXPRESSION is true or false and sets exit status. It is one of:\n\
987 "), stdout);
988 fputs (_("\
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\
994 "), stdout);
995 fputs (_("\
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\
1001 "), stdout);
1002 fputs (_("\
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\
1010 "), stdout);
1011 fputs (_("\
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\
1016 "), stdout);
1017 fputs (_("\
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\
1023 "), stdout);
1024 fputs (_("\
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\
1030 "), stdout);
1031 fputs (_("\
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\
1037 "), stdout);
1038 fputs (_("\
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\
1044 "), stdout);
1045 fputs (_("\
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\
1049 "), stdout);
1050 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1052 exit (status);
1054 #endif /* TEST_STANDALONE */
1056 #if !defined (TEST_STANDALONE)
1057 # define main test_command
1058 #endif
1060 #define AUTHORS "Kevin Braunsdorf", "Matthew Bradburn"
1063 * [:
1064 * '[' expr ']'
1065 * test:
1066 * test expr
1069 main (int margc, char **margv)
1071 int value;
1073 #if !defined (TEST_STANDALONE)
1074 int code;
1076 code = setjmp (test_exit_buf);
1078 if (code)
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 */
1091 argv = margv;
1093 if (LBRACKET)
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);
1107 --margc;
1110 argc = margc;
1111 pos = 1;
1113 if (pos >= argc)
1114 test_exit (SHELL_BOOLEAN (FALSE));
1116 value = posixtest (argc - 1);
1118 if (pos != argc)
1119 test_syntax_error (_("too many arguments\n"), NULL);
1121 test_exit (SHELL_BOOLEAN (value));