Update FSF postal mail address.
[platform/upstream/coreutils.git] / src / test.c
1 /* GNU test program (ksb and mjb) */
2
3 /* Modified to run with the GNU shell by bfox. */
4
5 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
6
7    This file is part of GNU Bash, the Bourne Again SHell.
8
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.
13
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.
18
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 02111-1307, USA.  */
22
23 /* Define TEST_STANDALONE to get the /bin/test version.  Otherwise, you get
24    the shell builtin version. */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29
30 #define TEST_STANDALONE 1
31
32 #ifndef LBRACKET
33 # define LBRACKET 0
34 #endif
35
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
42
43 #include "system.h"
44 #include "error.h"
45 #include "euidaccess.h"
46 #include "quote.h"
47
48 #ifndef _POSIX_VERSION
49 # include <sys/param.h>
50 #endif /* _POSIX_VERSION */
51 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
52 #define digit(c)  ((c) >= '0' && (c) <= '9')
53 #define digit_value(c) ((c) - '0')
54
55 char *program_name;
56
57 #if !defined (_POSIX_VERSION)
58 # include <sys/file.h>
59 #endif /* !_POSIX_VERSION */
60
61 extern gid_t getegid ();
62 extern uid_t geteuid ();
63
64 /* Exit status for syntax errors, etc.  */
65 enum { TEST_TRUE, TEST_FALSE, TEST_FAILURE };
66
67 #if defined (TEST_STANDALONE)
68 # define test_exit(val) exit (val)
69 #else
70    static jmp_buf test_exit_buf;
71    static int test_error_return = 0;
72 # define test_exit(val) test_error_return = val, longjmp (test_exit_buf, 1)
73 #endif /* !TEST_STANDALONE */
74
75 static int pos;         /* The offset of the current argument in ARGV. */
76 static int argc;        /* The number of arguments present in ARGV. */
77 static char **argv;     /* The argument list. */
78
79 static bool test_unop (char const *s);
80 static bool unary_operator (void);
81 static bool binary_operator (bool);
82 static bool two_arguments (void);
83 static bool three_arguments (void);
84 static bool posixtest (int);
85
86 static bool expr (void);
87 static bool term (void);
88 static bool and (void);
89 static bool or (void);
90
91 static void test_syntax_error (char const *format, char const *arg)
92      ATTRIBUTE_NORETURN;
93 static void beyond (void) ATTRIBUTE_NORETURN;
94
95 static void
96 test_syntax_error (char const *format, char const *arg)
97 {
98   fprintf (stderr, "%s: ", argv[0]);
99   fprintf (stderr, format, arg);
100   fflush (stderr);
101   test_exit (TEST_FAILURE);
102 }
103
104 /* Increment our position in the argument list.  Check that we're not
105    past the end of the argument list.  This check is supressed if the
106    argument is false.  */
107
108 static inline void
109 advance (bool f)
110 {
111   ++pos;
112
113   if (f && pos >= argc)
114     beyond ();
115 }
116
117 static inline void
118 unary_advance (void)
119 {
120   advance (true);
121   ++pos;
122 }
123
124 /*
125  * beyond - call when we're beyond the end of the argument list (an
126  *      error condition)
127  */
128 static void
129 beyond (void)
130 {
131   test_syntax_error (_("missing argument after %s"), quote (argv[argc - 1]));
132 }
133
134 /* Syntax error for when an integer argument was expected, but
135    something else was found. */
136 static void
137 integer_expected_error (char const *pch)
138 {
139   test_syntax_error (_("%s: integer expression expected\n"), pch);
140 }
141
142 /* Return true if the characters pointed to by STRING constitute a
143    valid number.  Stuff the converted number into RESULT if RESULT is
144    not null.  */
145 static bool
146 is_int (char const *string, intmax_t *result)
147 {
148   int sign;
149   intmax_t value;
150   char const *orig_string;
151
152   sign = 1;
153   value = 0;
154
155   if (result)
156     *result = 0;
157
158   /* Skip leading whitespace characters. */
159   while (whitespace (*string))
160     string++;
161
162   if (!*string)
163     return false;
164
165   /* Save a pointer to the start, for diagnostics.  */
166   orig_string = string;
167
168   /* We allow leading `-' or `+'. */
169   if (*string == '-' || *string == '+')
170     {
171       if (!digit (string[1]))
172         return false;
173
174       if (*string == '-')
175         sign = -1;
176
177       string++;
178     }
179
180   while (digit (*string))
181     {
182       if (result)
183         {
184           intmax_t new_v = 10 * value + sign * (*string - '0');
185           if (0 < sign
186               ? (INTMAX_MAX / 10 < value || new_v < 0)
187               : (value < INTMAX_MIN / 10 || 0 < new_v))
188             test_syntax_error ((0 < sign
189                                 ? _("integer is too large: %s\n")
190                                 : _("integer is too small: %s\n")),
191                                orig_string);
192           value = new_v;
193         }
194       string++;
195     }
196
197   /* Skip trailing whitespace, if any. */
198   while (whitespace (*string))
199     string++;
200
201   /* Error if not at end of string. */
202   if (*string)
203     return false;
204
205   if (result)
206     *result = value;
207
208   return true;
209 }
210
211 /* Find the modification time of FILE, and stuff it into *AGE.
212    Return true if successful.  */
213 static bool
214 age_of (char const *filename, time_t *age)
215 {
216   struct stat finfo;
217   bool ok = (stat (filename, &finfo) == 0);
218   if (ok)
219     *age = finfo.st_mtime;
220   return ok;
221 }
222
223 /* Return true if S is one of the test command's binary operators.  */
224 static bool
225 binop (char const *s)
226 {
227   return ((STREQ (s,   "=")) || (STREQ (s,  "!=")) || (STREQ (s, "-nt")) ||
228           (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
229           (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
230           (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
231 }
232
233 /*
234  * term - parse a term and return 1 or 0 depending on whether the term
235  *      evaluates to true or false, respectively.
236  *
237  * term ::=
238  *      '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
239  *      '-'('L'|'x') filename
240  *      '-t' int
241  *      '-'('z'|'n') string
242  *      string
243  *      string ('!='|'=') string
244  *      <int> '-'(eq|ne|le|lt|ge|gt) <int>
245  *      file '-'(nt|ot|ef) file
246  *      '(' <expr> ')'
247  * int ::=
248  *      '-l' string
249  *      positive and negative integers
250  */
251 static bool
252 term (void)
253 {
254   bool value;
255   bool negated = false;
256
257   /* Deal with leading `not's.  */
258   while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
259     {
260       advance (true);
261       negated = !negated;
262     }
263
264   if (pos >= argc)
265     beyond ();
266
267   /* A paren-bracketed argument. */
268   if (argv[pos][0] == '(' && argv[pos][1] == '\0')
269     {
270       int nargs;
271
272       advance (true);
273
274       for (nargs = 1;
275            pos + nargs < argc && ! STREQ (argv[pos + nargs], ")");
276            nargs++)
277         if (nargs == 4)
278           {
279             nargs = argc - pos;
280             break;
281           }
282
283       value = posixtest (nargs);
284       if (argv[pos] == 0)
285         test_syntax_error (_("')' expected\n"), NULL);
286       else
287         if (argv[pos][0] != ')' || argv[pos][1])
288           test_syntax_error (_("')' expected, found %s\n"), argv[pos]);
289       advance (false);
290     }
291
292   /* Are there enough arguments left that this could be dyadic?  */
293   else if (4 <= argc - pos && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))
294     value = binary_operator (true);
295   else if (3 <= argc - pos && binop (argv[pos + 1]))
296     value = binary_operator (false);
297
298   /* It might be a switch type argument.  */
299   else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
300     {
301       if (test_unop (argv[pos]))
302         value = unary_operator ();
303       else
304         test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
305     }
306   else
307     {
308       value = (argv[pos][0] != '\0');
309       advance (false);
310     }
311
312   return negated ^ value;
313 }
314
315 static bool
316 binary_operator (bool l_is_l)
317 {
318   int op;
319   struct stat stat_buf, stat_spare;
320   intmax_t l, r;
321   /* Is the right integer expression of the form '-l string'? */
322   bool r_is_l;
323
324   if (l_is_l)
325     advance (false);
326   op = pos + 1;
327
328   if ((op < argc - 2) && STREQ (argv[op + 1], "-l"))
329     {
330       r_is_l = true;
331       advance (false);
332     }
333   else
334     r_is_l = false;
335
336   if (argv[op][0] == '-')
337     {
338       /* check for eq, nt, and stuff */
339       switch (argv[op][1])
340         {
341         default:
342           break;
343
344         case 'l':
345           if (argv[op][2] == 't' && !argv[op][3])
346             {
347               /* lt */
348               if (l_is_l)
349                 l = strlen (argv[op - 1]);
350               else
351                 {
352                   if (!is_int (argv[op - 1], &l))
353                     integer_expected_error (_("before -lt"));
354                 }
355
356               if (r_is_l)
357                 r = strlen (argv[op + 2]);
358               else
359                 {
360                   if (!is_int (argv[op + 1], &r))
361                     integer_expected_error (_("after -lt"));
362                 }
363               pos += 3;
364               return l < r;
365             }
366
367           if (argv[op][2] == 'e' && !argv[op][3])
368             {
369               /* le */
370               if (l_is_l)
371                 l = strlen (argv[op - 1]);
372               else
373                 {
374                   if (!is_int (argv[op - 1], &l))
375                     integer_expected_error (_("before -le"));
376                 }
377               if (r_is_l)
378                 r = strlen (argv[op + 2]);
379               else
380                 {
381                   if (!is_int (argv[op + 1], &r))
382                     integer_expected_error (_("after -le"));
383                 }
384               pos += 3;
385               return l <= r;
386             }
387           break;
388
389         case 'g':
390           if (argv[op][2] == 't' && !argv[op][3])
391             {
392               /* gt integer greater than */
393               if (l_is_l)
394                 l = strlen (argv[op - 1]);
395               else
396                 {
397                   if (!is_int (argv[op - 1], &l))
398                     integer_expected_error (_("before -gt"));
399                 }
400               if (r_is_l)
401                 r = strlen (argv[op + 2]);
402               else
403                 {
404                   if (!is_int (argv[op + 1], &r))
405                     integer_expected_error (_("after -gt"));
406                 }
407               pos += 3;
408               return l > r;
409             }
410
411           if (argv[op][2] == 'e' && !argv[op][3])
412             {
413               /* ge - integer greater than or equal to */
414               if (l_is_l)
415                 l = strlen (argv[op - 1]);
416               else
417                 {
418                   if (!is_int (argv[op - 1], &l))
419                     integer_expected_error (_("before -ge"));
420                 }
421               if (r_is_l)
422                 r = strlen (argv[op + 2]);
423               else
424                 {
425                   if (!is_int (argv[op + 1], &r))
426                     integer_expected_error (_("after -ge"));
427                 }
428               pos += 3;
429               return l >= r;
430             }
431           break;
432
433         case 'n':
434           if (argv[op][2] == 't' && !argv[op][3])
435             {
436               /* nt - newer than */
437               time_t lt IF_LINT (= 0);
438               time_t rt IF_LINT (= 0);
439               bool le, re;
440               pos += 3;
441               if (l_is_l | r_is_l)
442                 test_syntax_error (_("-nt does not accept -l\n"), NULL);
443               le = age_of (argv[op - 1], &lt);
444               re = age_of (argv[op + 1], &rt);
445               return le > re || (le && lt > rt);
446             }
447
448           if (argv[op][2] == 'e' && !argv[op][3])
449             {
450               /* ne - integer not equal */
451               if (l_is_l)
452                 l = strlen (argv[op - 1]);
453               else
454                 {
455                   if (!is_int (argv[op - 1], &l))
456                     integer_expected_error (_("before -ne"));
457                 }
458               if (r_is_l)
459                 r = strlen (argv[op + 2]);
460               else
461                 {
462                   if (!is_int (argv[op + 1], &r))
463                     integer_expected_error (_("after -ne"));
464                 }
465               pos += 3;
466               return l != r;
467             }
468           break;
469
470         case 'e':
471           if (argv[op][2] == 'q' && !argv[op][3])
472             {
473               /* eq - integer equal */
474               if (l_is_l)
475                 l = strlen (argv[op - 1]);
476               else
477                 {
478                   if (!is_int (argv[op - 1], &l))
479                     integer_expected_error (_("before -eq"));
480                 }
481               if (r_is_l)
482                 r = strlen (argv[op + 2]);
483               else
484                 {
485                   if (!is_int (argv[op + 1], &r))
486                     integer_expected_error (_("after -eq"));
487                 }
488               pos += 3;
489               return l == r;
490             }
491
492           if (argv[op][2] == 'f' && !argv[op][3])
493             {
494               /* ef - hard link? */
495               pos += 3;
496               if (l_is_l | r_is_l)
497                 test_syntax_error (_("-ef does not accept -l\n"), NULL);
498               return (stat (argv[op - 1], &stat_buf) == 0
499                       && stat (argv[op + 1], &stat_spare) == 0
500                       && stat_buf.st_dev == stat_spare.st_dev
501                       && stat_buf.st_ino == stat_spare.st_ino);
502             }
503           break;
504
505         case 'o':
506           if ('t' == argv[op][2] && '\000' == argv[op][3])
507             {
508               /* ot - older than */
509               time_t lt IF_LINT (= 0);
510               time_t rt IF_LINT (= 0);
511               bool le, re;
512               pos += 3;
513               if (l_is_l | r_is_l)
514                 test_syntax_error (_("-ot does not accept -l\n"), NULL);
515               le = age_of (argv[op - 1], &lt);
516               re = age_of (argv[op + 1], &rt);
517               return le < re || (re && lt < rt);
518             }
519           break;
520         }
521
522       /* FIXME: is this dead code? */
523       test_syntax_error (_("unknown binary operator\n"), argv[op]);
524     }
525
526   if (argv[op][0] == '=' && !argv[op][1])
527     {
528       bool value = STREQ (argv[pos], argv[pos + 2]);
529       pos += 3;
530       return value;
531     }
532
533   if (STREQ (argv[op], "!="))
534     {
535       bool value = !STREQ (argv[pos], argv[pos + 2]);
536       pos += 3;
537       return value;
538     }
539
540   /* Not reached.  */
541   abort ();
542 }
543
544 static bool
545 unary_operator (void)
546 {
547   struct stat stat_buf;
548
549   switch (argv[pos][1])
550     {
551     default:
552       return false;
553
554       /* All of the following unary operators use unary_advance (), which
555          checks to make sure that there is an argument, and then advances
556          pos right past it.  This means that pos - 1 is the location of the
557          argument. */
558
559     case 'a':                   /* file exists in the file system? */
560     case 'e':
561       unary_advance ();
562       return stat (argv[pos - 1], &stat_buf) == 0;
563
564     case 'r':                   /* file is readable? */
565       unary_advance ();
566       return euidaccess (argv[pos - 1], R_OK) == 0;
567
568     case 'w':                   /* File is writable? */
569       unary_advance ();
570       return euidaccess (argv[pos - 1], W_OK) == 0;
571
572     case 'x':                   /* File is executable? */
573       unary_advance ();
574       return euidaccess (argv[pos - 1], X_OK) == 0;
575
576     case 'O':                   /* File is owned by you? */
577       unary_advance ();
578       return (stat (argv[pos - 1], &stat_buf) == 0
579               && (geteuid () == stat_buf.st_uid));
580
581     case 'G':                   /* File is owned by your group? */
582       unary_advance ();
583       return (stat (argv[pos - 1], &stat_buf) == 0
584               && (getegid () == stat_buf.st_gid));
585
586     case 'f':                   /* File is a file? */
587       unary_advance ();
588       /* Under POSIX, -f is true if the given file exists
589          and is a regular file. */
590       return (stat (argv[pos - 1], &stat_buf) == 0
591               && S_ISREG (stat_buf.st_mode));
592
593     case 'd':                   /* File is a directory? */
594       unary_advance ();
595       return (stat (argv[pos - 1], &stat_buf) == 0
596               && S_ISDIR (stat_buf.st_mode));
597
598     case 's':                   /* File has something in it? */
599       unary_advance ();
600       return (stat (argv[pos - 1], &stat_buf) == 0
601               && 0 < stat_buf.st_size);
602
603     case 'S':                   /* File is a socket? */
604       unary_advance ();
605       return (stat (argv[pos - 1], &stat_buf) == 0
606               && S_ISSOCK (stat_buf.st_mode));
607
608     case 'c':                   /* File is character special? */
609       unary_advance ();
610       return (stat (argv[pos - 1], &stat_buf) == 0
611               && S_ISCHR (stat_buf.st_mode));
612
613     case 'b':                   /* File is block special? */
614       unary_advance ();
615       return (stat (argv[pos - 1], &stat_buf) == 0
616               && S_ISBLK (stat_buf.st_mode));
617
618     case 'p':                   /* File is a named pipe? */
619       unary_advance ();
620       return (stat (argv[pos - 1], &stat_buf) == 0
621               && S_ISFIFO (stat_buf.st_mode));
622
623     case 'L':                   /* Same as -h  */
624       /*FALLTHROUGH*/
625
626     case 'h':                   /* File is a symbolic link? */
627       unary_advance ();
628       return (lstat (argv[pos - 1], &stat_buf) == 0
629               && S_ISLNK (stat_buf.st_mode));
630
631     case 'u':                   /* File is setuid? */
632       unary_advance ();
633       return (stat (argv[pos - 1], &stat_buf) == 0
634               && (stat_buf.st_mode & S_ISUID));
635
636     case 'g':                   /* File is setgid? */
637       unary_advance ();
638       return (stat (argv[pos - 1], &stat_buf) == 0
639               && (stat_buf.st_mode & S_ISGID));
640
641     case 'k':                   /* File has sticky bit set? */
642       unary_advance ();
643       return (stat (argv[pos - 1], &stat_buf) == 0
644               && (stat_buf.st_mode & S_ISVTX));
645
646     case 't':                   /* File (fd) is a terminal? */
647       {
648         intmax_t fd;
649         unary_advance ();
650         if (!is_int (argv[pos - 1], &fd))
651           integer_expected_error (_("after -t"));
652         return INT_MIN <= fd && fd <= INT_MAX && isatty (fd);
653       }
654
655     case 'n':                   /* True if arg has some length. */
656       unary_advance ();
657       return argv[pos - 1][0] != 0;
658
659     case 'z':                   /* True if arg has no length. */
660       unary_advance ();
661       return argv[pos - 1][0] == '\0';
662     }
663 }
664
665 /*
666  * and:
667  *      term
668  *      term '-a' and
669  */
670 static bool
671 and (void)
672 {
673   bool value = true;
674
675   for (;;)
676     {
677       value &= term ();
678       if (! (pos < argc && STREQ (argv[pos], "-a")))
679         return value;
680       advance (false);
681     }
682 }
683
684 /*
685  * or:
686  *      and
687  *      and '-o' or
688  */
689 static bool
690 or (void)
691 {
692   bool value = false;
693
694   for (;;)
695     {
696       value |= and ();
697       if (! (pos < argc && STREQ (argv[pos], "-o")))
698         return value;
699       advance (false);
700     }
701 }
702
703 /*
704  * expr:
705  *      or
706  */
707 static bool
708 expr (void)
709 {
710   if (pos >= argc)
711     beyond ();
712
713   return or ();         /* Same with this. */
714 }
715
716 /* Return true if OP is one of the test command's unary operators. */
717 static bool
718 test_unop (char const *op)
719 {
720   if (op[0] != '-')
721     return false;
722
723   switch (op[1])
724     {
725     case 'a': case 'b': case 'c': case 'd': case 'e':
726     case 'f': case 'g': case 'h': case 'k': case 'n':
727     case 'o': case 'p': case 'r': case 's': case 't':
728     case 'u': case 'w': case 'x': case 'z':
729     case 'G': case 'L': case 'O': case 'S': case 'N':
730       return true;
731     }
732
733   return false;
734 }
735
736 static bool
737 one_argument (void)
738 {
739   return argv[pos++][0] != '\0';
740 }
741
742 static bool
743 two_arguments (void)
744 {
745   bool value;
746
747   if (STREQ (argv[pos], "!"))
748     {
749       advance (false);
750       value = ! one_argument ();
751     }
752   else if (argv[pos][0] == '-'
753            && argv[pos][1] != '\0'
754            && argv[pos][2] == '\0')
755     {
756       if (test_unop (argv[pos]))
757         value = unary_operator ();
758       else
759         test_syntax_error (_("%s: unary operator expected\n"), argv[pos]);
760     }
761   else
762     beyond ();
763   return (value);
764 }
765
766 static bool
767 three_arguments (void)
768 {
769   bool value;
770
771   if (binop (argv[pos + 1]))
772     value = binary_operator (false);
773   else if (STREQ (argv[pos], "!"))
774     {
775       advance (true);
776       value = !two_arguments ();
777     }
778   else if (STREQ (argv[pos], "(") && STREQ (argv[pos + 2], ")"))
779     {
780       advance (false);
781       value = one_argument ();
782       advance (false);
783     }
784   else if (STREQ (argv[pos + 1], "-a") || STREQ (argv[pos + 1], "-o"))
785     value = expr ();
786   else
787     test_syntax_error (_("%s: binary operator expected\n"), argv[pos+1]);
788   return (value);
789 }
790
791 /* This is an implementation of a Posix.2 proposal by David Korn. */
792 static bool
793 posixtest (int nargs)
794 {
795   bool value;
796
797   switch (nargs)
798     {
799       case 1:
800         value = one_argument ();
801         break;
802
803       case 2:
804         value = two_arguments ();
805         break;
806
807       case 3:
808         value = three_arguments ();
809         break;
810
811       case 4:
812         if (STREQ (argv[pos], "!"))
813           {
814             advance (true);
815             value = !three_arguments ();
816             break;
817           }
818         if (STREQ (argv[pos], "(") && STREQ (argv[pos + 3], ")"))
819           {
820             advance (false);
821             value = two_arguments ();
822             advance (false);
823             break;
824           }
825         /* FALLTHROUGH */
826       case 5:
827       default:
828         if (nargs <= 0)
829           abort ();
830         value = expr ();
831     }
832
833   return (value);
834 }
835
836 #if defined (TEST_STANDALONE)
837 # include "long-options.h"
838
839 void
840 usage (int status)
841 {
842   if (status != EXIT_SUCCESS)
843     fprintf (stderr, _("Try `%s --help' for more information.\n"),
844              program_name);
845   else
846     {
847       fputs (_("\
848 Usage: test EXPRESSION\n\
849   or:  test\n\
850   or:  [ EXPRESSION ]\n\
851   or:  [ ]\n\
852   or:  [ OPTION\n\
853 "), stdout);
854       fputs (_("\
855 Exit with the status determined by EXPRESSION.\n\
856 \n\
857 "), stdout);
858       fputs (HELP_OPTION_DESCRIPTION, stdout);
859       fputs (VERSION_OPTION_DESCRIPTION, stdout);
860       fputs (_("\
861 \n\
862 An omitted EXPRESSION defaults to false.  Otherwise,\n\
863 EXPRESSION is true or false and sets exit status.  It is one of:\n\
864 "), stdout);
865       fputs (_("\
866 \n\
867   ( EXPRESSION )               EXPRESSION is true\n\
868   ! EXPRESSION                 EXPRESSION is false\n\
869   EXPRESSION1 -a EXPRESSION2   both EXPRESSION1 and EXPRESSION2 are true\n\
870   EXPRESSION1 -o EXPRESSION2   either EXPRESSION1 or EXPRESSION2 is true\n\
871 "), stdout);
872       fputs (_("\
873 \n\
874   -n STRING            the length of STRING is nonzero\n\
875   STRING               equivalent to -n STRING\n\
876   -z STRING            the length of STRING is zero\n\
877   STRING1 = STRING2    the strings are equal\n\
878   STRING1 != STRING2   the strings are not equal\n\
879 "), stdout);
880       fputs (_("\
881 \n\
882   INTEGER1 -eq INTEGER2   INTEGER1 is equal to INTEGER2\n\
883   INTEGER1 -ge INTEGER2   INTEGER1 is greater than or equal to INTEGER2\n\
884   INTEGER1 -gt INTEGER2   INTEGER1 is greater than INTEGER2\n\
885   INTEGER1 -le INTEGER2   INTEGER1 is less than or equal to INTEGER2\n\
886   INTEGER1 -lt INTEGER2   INTEGER1 is less than INTEGER2\n\
887   INTEGER1 -ne INTEGER2   INTEGER1 is not equal to INTEGER2\n\
888 "), stdout);
889       fputs (_("\
890 \n\
891   FILE1 -ef FILE2   FILE1 and FILE2 have the same device and inode numbers\n\
892   FILE1 -nt FILE2   FILE1 is newer (modification date) than FILE2\n\
893   FILE1 -ot FILE2   FILE1 is older than FILE2\n\
894 "), stdout);
895       fputs (_("\
896 \n\
897   -b FILE     FILE exists and is block special\n\
898   -c FILE     FILE exists and is character special\n\
899   -d FILE     FILE exists and is a directory\n\
900   -e FILE     FILE exists\n\
901 "), stdout);
902       fputs (_("\
903   -f FILE     FILE exists and is a regular file\n\
904   -g FILE     FILE exists and is set-group-ID\n\
905   -G FILE     FILE exists and is owned by the effective group ID\n\
906   -h FILE     FILE exists and is a symbolic link (same as -L)\n\
907   -k FILE     FILE exists and has its sticky bit set\n\
908 "), stdout);
909       fputs (_("\
910   -L FILE     FILE exists and is a symbolic link (same as -h)\n\
911   -O FILE     FILE exists and is owned by the effective user ID\n\
912   -p FILE     FILE exists and is a named pipe\n\
913   -r FILE     FILE exists and read permission is granted\n\
914   -s FILE     FILE exists and has a size greater than zero\n\
915 "), stdout);
916       fputs (_("\
917   -S FILE     FILE exists and is a socket\n\
918   -t FD       file descriptor FD is opened on a terminal\n\
919   -u FILE     FILE exists and its set-user-ID bit is set\n\
920   -w FILE     FILE exists and write permission is granted\n\
921   -x FILE     FILE exists and execute (or search) permission is granted\n\
922 "), stdout);
923       fputs (_("\
924 \n\
925 Except for -h and -L, all FILE-related tests dereference symbolic links.\n\
926 Beware that parentheses need to be escaped (e.g., by backslashes) for shells.\n\
927 INTEGER may also be -l STRING, which evaluates to the length of STRING.\n\
928 "), stdout);
929       printf (USAGE_BUILTIN_WARNING, _("test and/or ["));
930       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
931     }
932   exit (status);
933 }
934 #endif /* TEST_STANDALONE */
935
936 #if !defined (TEST_STANDALONE)
937 # define main test_command
938 #endif
939
940 #define AUTHORS "Kevin Braunsdorf", "Matthew Bradburn"
941
942 /*
943  * [:
944  *      '[' expr ']'
945  * test:
946  *      test expr
947  */
948 int
949 main (int margc, char **margv)
950 {
951   bool value;
952
953 #if !defined (TEST_STANDALONE)
954   int code;
955
956   code = setjmp (test_exit_buf);
957
958   if (code)
959     return (test_error_return);
960 #else /* TEST_STANDALONE */
961   initialize_main (&margc, &margv);
962   program_name = margv[0];
963   setlocale (LC_ALL, "");
964   bindtextdomain (PACKAGE, LOCALEDIR);
965   textdomain (PACKAGE);
966
967   initialize_exit_failure (TEST_FAILURE);
968   atexit (close_stdout);
969 #endif /* TEST_STANDALONE */
970
971   argv = margv;
972
973   if (LBRACKET)
974     {
975       /* Recognize --help or --version, but only when invoked in the
976          "[" form, and when the last argument is not "]".  POSIX
977          allows "[ --help" and "[ --version" to have the usual GNU
978          behavior, but it requires "test --help" and "test --version"
979          to exit silently with status 1.  */
980       if (margc < 2 || !STREQ (margv[margc - 1], "]"))
981         {
982           parse_long_options (margc, margv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
983                               usage, AUTHORS, (char const *) NULL);
984           test_syntax_error (_("missing `]'\n"), NULL);
985         }
986
987       --margc;
988     }
989
990   argc = margc;
991   pos = 1;
992
993   if (pos >= argc)
994     test_exit (TEST_FALSE);
995
996   value = posixtest (argc - 1);
997
998   if (pos != argc)
999     test_syntax_error (_("extra argument %s"), quote (argv[pos]));
1000
1001   test_exit (value ? TEST_TRUE : TEST_FALSE);
1002 }