]>
git.ipfire.org Git - thirdparty/bash.git/blob - test.c
1 /* GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987, 1988, 1989, 1990, 1991 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 along
20 with Bash; see the file COPYING. If not, write to the Free Software
21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
25 /* #define PATTERN_MATCHING */
27 #if defined (HAVE_CONFIG_H)
33 #include "bashtypes.h"
35 #if defined (HAVE_LIMITS_H)
38 # include <sys/param.h>
41 #if defined (HAVE_UNISTD_H)
50 #if !defined (_POSIX_VERSION)
51 # include <sys/file.h>
52 #endif /* !_POSIX_VERSION */
53 #include "posixstat.h"
57 #include "builtins/common.h"
60 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
64 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
68 # define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0)
89 /* The following few defines control the truth and false output of each stage.
90 TRUE and FALSE are what we use to compute the final output value.
91 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
92 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
95 #define SHELL_BOOLEAN(value) (!(value))
97 static procenv_t test_exit_buf
;
98 static int test_error_return
;
99 #define test_exit(val) \
100 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
103 /* We have to use access(2) for machines running AFS, because it's
104 not a Unix file system. This may produce incorrect answers for
105 non-AFS files. I hate AFS. */
106 # define EACCESS(path, mode) access(path, mode)
108 # define EACCESS(path, mode) test_eaccess(path, mode)
111 static int pos
; /* The offset of the current argument in ARGV. */
112 static int argc
; /* The number of arguments present in ARGV. */
113 static char **argv
; /* The argument list. */
118 static int unary_operator ();
119 static int binary_operator ();
120 static int two_arguments ();
121 static int three_arguments ();
122 static int posixtest ();
129 static void beyond ();
132 test_syntax_error (format
, arg
)
135 extern int interactive_shell
;
136 extern char *get_name_for_error ();
137 if (interactive_shell
== 0)
138 fprintf (stderr
, "%s: ", get_name_for_error ());
139 fprintf (stderr
, "%s: ", argv
[0]);
140 fprintf (stderr
, format
, arg
);
141 fprintf (stderr
, "\n");
143 test_exit (SHELL_BOOLEAN (FALSE
));
146 /* A wrapper for stat () which disallows pathnames that are empty strings
147 and handles /dev/fd emulation on systems that don't have it. */
149 test_stat (path
, finfo
)
158 if (path
[0] == '/' && path
[1] == 'd' && strncmp (path
, "/dev/fd/", 8) == 0)
160 #if !defined (HAVE_DEV_FD)
162 if (legal_number (path
+ 8, &fd
))
163 return (fstat ((int)fd
, finfo
));
170 /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
171 trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
172 On most systems, with the notable exception of linux, this is
173 effectively a no-op. */
175 strcpy (pbuf
, DEV_FD_PREFIX
);
176 strcat (pbuf
, path
+ 8);
177 return (stat (pbuf
, finfo
));
178 #endif /* !HAVE_DEV_FD */
180 return (stat (path
, finfo
));
183 /* Do the same thing access(2) does, but use the effective uid and gid,
184 and don't make the mistake of telling root that any file is
187 test_eaccess (path
, mode
)
193 if (test_stat (path
, &st
) < 0)
196 if (current_user
.euid
== 0)
198 /* Root can read or write any file. */
202 /* Root can execute any file that has any one of the execute
204 if (st
.st_mode
& S_IXUGO
)
208 if (st
.st_uid
== current_user
.euid
) /* owner */
210 else if (group_member (st
.st_gid
))
213 if (st
.st_mode
& mode
)
219 /* Increment our position in the argument list. Check that we're not
220 past the end of the argument list. This check is supressed if the
221 argument is FALSE. Made a macro for efficiency. */
222 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
223 #define unary_advance() do { advance (1); ++pos; } while (0)
226 * beyond - call when we're beyond the end of the argument list (an
232 test_syntax_error ("argument expected", (char *)NULL
);
235 /* Syntax error for when an integer argument was expected, but
236 something else was found. */
238 integer_expected_error (pch
)
241 test_syntax_error ("%s: integer expression expected", pch
);
245 * term - parse a term and return 1 or 0 depending on whether the term
246 * evaluates to true or false, respectively.
249 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'p'|'r'|'s'|'u'|'w'|'x') filename
250 * '-'('G'|'L'|'O'|'S') filename
252 * '-'('z'|'n') string
254 * string ('!='|'='|'==') string
255 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
256 * file '-'(nt|ot|ef) file
259 * positive and negative integers
269 /* Deal with leading `not's. */
270 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
273 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
279 return (value
? !term() : term());
282 /* A paren-bracketed argument. */
283 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0')
288 test_syntax_error ("`)' expected", (char *)NULL
);
289 else if (argv
[pos
][0] != ')' || argv
[pos
][1])
290 test_syntax_error ("`)' expected, found %s", argv
[pos
]);
296 /* are there enough arguments left that this could be dyadic? */
297 if ((pos
+ 3 <= argc
) && binop (argv
[pos
+ 1]))
298 value
= binary_operator ();
300 /* If this is supposed to be a binary operator, make sure there are
301 enough arguments and fail if there are not. */
302 if ((pos
+ 1 < argc
) && binop (argv
[pos
+1]))
305 value
= binary_operator ();
311 /* Might be a switch type argument */
312 else if (argv
[pos
][0] == '-' && argv
[pos
][2] == '\0')
314 if (unop (argv
[pos
][1]))
315 value
= unary_operator ();
317 test_syntax_error ("%s: unary operator expected", argv
[pos
]);
321 value
= argv
[pos
][0] != '\0';
333 struct stat st1
, st2
;
335 if (test_stat (s
, &st1
) < 0 || test_stat (t
, &st2
) < 0)
339 case OT
: return (st1
.st_mtime
< st2
.st_mtime
);
340 case NT
: return (st1
.st_mtime
> st2
.st_mtime
);
341 case EF
: return ((st1
.st_dev
== st2
.st_dev
) && (st1
.st_ino
== st2
.st_ino
));
353 if (legal_number (s
, &l
) == 0)
354 integer_expected_error (s
);
355 if (legal_number (t
, &r
) == 0)
356 integer_expected_error (t
);
359 case EQ
: return (l
== r
);
360 case NE
: return (l
!= r
);
361 case LT
: return (l
< r
);
362 case GT
: return (l
> r
);
363 case LE
: return (l
<= r
);
364 case GE
: return (l
>= r
);
369 #if defined (PATTERN_MATCHING)
371 patcomp (string
, pat
, op
)
377 m
= fnmatch (pat
, string
, 0);
380 case EQ
: return (m
== 0);
381 case NE
: return (m
!= 0);
384 #endif /* PATTERN_MATCHING */
393 if (w
[0] == '=' && (w
[1] == '\0' || (w
[1] == '=' && w
[2] == '\0')))
395 value
= STREQ (argv
[pos
], argv
[pos
+ 2]);
399 if ((w
[0] == '>' || w
[0] == '<') && w
[1] == '\0')
401 value
= (w
[0] == '>') ? strcmp (argv
[pos
], argv
[pos
+ 2]) > 0
402 : strcmp (argv
[pos
], argv
[pos
+ 2]) < 0;
406 #if defined (PATTERN_MATCHING)
407 if ((w
[0] == '=' || w
[0] == '!') && w
[1] == '~' && w
[2] == '\0')
409 value
= patcomp (argv
[pos
], argv
[pos
+ 2], w
[0] == '=' ? EQ
: NE
);
414 if (w
[0] == '!' && w
[1] == '=' && w
[2] == '\0')
416 value
= STREQ (argv
[pos
], argv
[pos
+ 2]) == 0;
421 if (w
[0] != '-' || w
[3] != '\0')
423 test_syntax_error ("%s: binary operator expected", w
);
433 case 'n': value
= filecomp (argv
[pos
], argv
[pos
+ 2], NT
); break;
434 case 'o': value
= filecomp (argv
[pos
], argv
[pos
+ 2], OT
); break;
435 case 'l': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], LT
); break;
436 case 'g': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], GT
); break;
437 default: test_syntax_error ("-%s: binary operator expected", w
);
440 else if (w
[0] == 'e')
444 case 'q': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], EQ
); break;
445 case 'f': value
= filecomp (argv
[pos
], argv
[pos
+ 2], EF
); break;
446 default: test_syntax_error ("-%s: binary operator expected", w
);
449 else if (w
[1] == 'e')
453 case 'n': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], NE
); break;
454 case 'g': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], GE
); break;
455 case 'l': value
= arithcomp (argv
[pos
], argv
[pos
+ 2], LE
); break;
456 default: test_syntax_error ("-%s: binary operator expected", w
);
460 test_syntax_error ("-%s: binary operator expected", w
);
470 struct stat stat_buf
;
472 switch (argv
[pos
][1])
477 /* All of the following unary operators use unary_advance (), which
478 checks to make sure that there is an argument, and then advances
479 pos right past it. This means that pos - 1 is the location of the
482 case 'a': /* file exists in the file system? */
485 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0);
487 case 'r': /* file is readable? */
489 return (EACCESS (argv
[pos
- 1], R_OK
) == 0);
491 case 'w': /* File is writeable? */
493 return (EACCESS (argv
[pos
- 1], W_OK
) == 0);
495 case 'x': /* File is executable? */
497 return (EACCESS (argv
[pos
- 1], X_OK
) == 0);
499 case 'O': /* File is owned by you? */
501 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
502 (uid_t
) current_user
.euid
== (uid_t
) stat_buf
.st_uid
);
504 case 'G': /* File is owned by your group? */
506 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
507 (gid_t
) current_user
.egid
== (gid_t
) stat_buf
.st_gid
);
509 case 'f': /* File is a file? */
511 if (test_stat (argv
[pos
- 1], &stat_buf
) < 0)
514 /* -f is true if the given file exists and is a regular file. */
516 return (S_ISREG (stat_buf
.st_mode
) || (stat_buf
.st_mode
& S_IFMT
) == 0);
518 return (S_ISREG (stat_buf
.st_mode
));
521 case 'd': /* File is a directory? */
523 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
524 (S_ISDIR (stat_buf
.st_mode
)));
526 case 's': /* File has something in it? */
528 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
529 stat_buf
.st_size
> (off_t
) 0);
531 case 'S': /* File is a socket? */
532 #if !defined (S_ISSOCK)
536 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
537 S_ISSOCK (stat_buf
.st_mode
));
538 #endif /* S_ISSOCK */
540 case 'c': /* File is character special? */
542 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
543 S_ISCHR (stat_buf
.st_mode
));
545 case 'b': /* File is block special? */
547 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
548 S_ISBLK (stat_buf
.st_mode
));
550 case 'p': /* File is a named pipe? */
555 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
556 S_ISFIFO (stat_buf
.st_mode
));
557 #endif /* S_ISFIFO */
559 case 'L': /* Same as -h */
560 case 'h': /* File is a symbolic link? */
562 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
565 return ((argv
[pos
- 1][0] != '\0') &&
566 (lstat (argv
[pos
- 1], &stat_buf
) == 0) &&
567 S_ISLNK (stat_buf
.st_mode
));
568 #endif /* S_IFLNK && HAVE_LSTAT */
570 case 'u': /* File is setuid? */
572 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
573 (stat_buf
.st_mode
& S_ISUID
) != 0);
575 case 'g': /* File is setgid? */
577 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
578 (stat_buf
.st_mode
& S_ISGID
) != 0);
580 case 'k': /* File has sticky bit set? */
582 #if !defined (S_ISVTX)
583 /* This is not Posix, and is not defined on some Posix systems. */
586 return (test_stat (argv
[pos
- 1], &stat_buf
) == 0 &&
587 (stat_buf
.st_mode
& S_ISVTX
) != 0);
590 case 't': /* File fd is a terminal? fd defaults to stdout. */
592 if (pos
< argc
&& legal_number (argv
[pos
], &r
))
595 return (isatty ((int)r
));
599 case 'n': /* True if arg has some length. */
601 return (argv
[pos
- 1][0] != '\0');
603 case 'z': /* True if arg has no length. */
605 return (argv
[pos
- 1][0] == '\0');
609 return (minus_o_option_value (argv
[pos
- 1]) == 1);
624 while (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'a' && !argv
[pos
][2])
628 return (value
&& v2
);
644 while (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'o' && !argv
[pos
][2])
648 return (value
|| v2
);
664 return (FALSE
^ or ()); /* Same with this. */
667 /* Return TRUE if S is one of the test command's binary operators. */
674 if (s
[0] == '=' && s
[1] == '\0')
675 return (1); /* '=' */
676 else if ((s
[0] == '<' || s
[0] == '>') && s
[1] == '\0') /* string <, > */
678 else if ((s
[0] == '=' || s
[0] == '!') && s
[1] == '=' && s
[2] == '\0')
679 return (1); /* `==' and `!=' */
680 #if defined (PATTERN_MATCHING)
681 else if (s
[2] == '\0' && s
[1] == '~' && (s
[0] == '=' || s
[0] == '!'))
684 else if (s
[0] != '-' || s
[2] == '\0' || s
[3] != '\0')
700 else if (t
[0] == 'e')
709 else if (t
[1] == 'e')
724 /* Return non-zero if OP is one of the test command's unary operators. */
731 case 'a': case 'b': case 'c': case 'd': case 'e':
732 case 'f': case 'g': case 'h': case 'k': case 'n':
733 case 'p': case 'r': case 's': case 't': case 'u':
734 case 'w': case 'x': case 'z':
735 case 'G': case 'L': case 'O': case 'S':
745 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
746 return (argv
[pos
+ 1][0] == '\0');
747 else if (argv
[pos
][0] == '-' && argv
[pos
][2] == '\0')
749 if (unop (argv
[pos
][1]))
750 return (unary_operator ());
752 test_syntax_error ("%s: unary operator expected", argv
[pos
]);
755 test_syntax_error ("%s: unary operator expected", argv
[pos
]);
760 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
762 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
769 if (binop (argv
[pos
+1]))
771 value
= binary_operator ();
774 else if (ANDOR (argv
[pos
+1]))
776 if (argv
[pos
+1][1] == 'a')
777 value
= ONE_ARG_TEST(argv
[pos
]) && ONE_ARG_TEST(argv
[pos
+2]);
779 value
= ONE_ARG_TEST(argv
[pos
]) || ONE_ARG_TEST(argv
[pos
+2]);
782 else if (argv
[pos
][0] == '!' && !argv
[pos
][1])
785 value
= !two_arguments ();
787 else if (argv
[pos
][0] == '(' && argv
[pos
+2][0] == ')')
789 value
= ONE_ARG_TEST(argv
[pos
+1]);
793 test_syntax_error ("%s: binary operator expected", argv
[pos
+1]);
798 /* This is an implementation of a Posix.2 proposal by David Korn. */
804 switch (argc
- 1) /* one extra passed in */
812 value
= ONE_ARG_TEST(argv
[1]);
817 value
= two_arguments ();
822 value
= three_arguments ();
826 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
829 value
= !three_arguments ();
847 test_command (margc
, margv
)
855 code
= setjmp (test_exit_buf
);
858 return (test_error_return
);
862 if (margv
[0] && margv
[0][0] == '[' && margv
[0][1] == '\0')
867 test_exit (SHELL_BOOLEAN (FALSE
));
869 if (margv
[margc
] && (margv
[margc
][0] != ']' || margv
[margc
][1]))
870 test_syntax_error ("missing `]'", (char *)NULL
);
877 test_exit (SHELL_BOOLEAN (FALSE
));
880 value
= posixtest ();
883 test_syntax_error ("too many arguments", (char *)NULL
);
885 test_exit (SHELL_BOOLEAN (value
));