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 STANDALONE to get the /bin/test version. Otherwise, you get
24 the shell builtin version. */
25 /* #define STANDALONE */
27 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
29 /* #define PATTERN_MATCHING */
31 #if defined (HAVE_CONFIG_H)
37 #if defined (STANDALONE)
38 # include <sys/types.h>
40 # include "bashtypes.h"
43 #if defined (HAVE_LIMITS_H)
46 # include <sys/param.h>
49 #if defined (HAVE_UNISTD_H)
53 #if !defined (_POSIX_VERSION)
54 # include <sys/file.h>
55 #endif /* !_POSIX_VERSION */
56 #include "posixstat.h"
59 #if !defined (STANDALONE)
61 # include "builtins/common.h"
62 # define main test_command
63 # define isint legal_number
64 # define getuid() current_user.uid
65 # define geteuid() current_user.euid
66 # define getgid() current_user.gid
67 # define getegid() current_user.egid
68 #else /* STANDALONE */
69 # if !defined (S_IXUGO)
72 # if defined (HAVE_UNISTD_H)
74 # endif /* HAVE_UNISTD_H */
75 # define whitespace(c) (((c) == ' ') || ((c) == '\t'))
76 # define digit(c) ((c) >= '0' && (c) <= '9')
77 # define digit_value(c) ((c) - '0')
78 #endif /* STANDALONE */
81 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
90 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
94 # define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0)
97 /* Make gid_t and uid_t mean something for non-posix systems. */
98 #if defined (STANDALONE) && !defined (_POSIX_VERSION) && !defined (HAVE_UID_T)
102 # if !defined (uid_t)
105 #endif /* STANDALONE && !_POSIX_VERSION && !HAVE_UID_T */
125 /* The following few defines control the truth and false output of each stage.
126 TRUE and FALSE are what we use to compute the final output value.
127 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
128 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
131 #define SHELL_BOOLEAN(value) (!(value))
133 #if defined (STANDALONE)
134 # define test_exit(val) exit (val)
136 static procenv_t test_exit_buf;
137 static int test_error_return;
138 # define test_exit(val) \
139 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
140 #endif /* STANDALONE */
143 /* We have to use access(2) for machines running AFS, because it's
144 not a Unix file system. This may produce incorrect answers for
145 non-AFS files. I hate AFS. */
146 # define EACCESS(path, mode) access(path, mode)
148 # define EACCESS(path, mode) test_eaccess(path, mode)
151 static int pos; /* The offset of the current argument in ARGV. */
152 static int argc; /* The number of arguments present in ARGV. */
153 static char **argv; /* The argument list. */
156 #if defined (STANDALONE)
161 static int unary_operator ();
162 static int binary_operator ();
163 static int two_arguments ();
164 static int three_arguments ();
165 static int posixtest ();
172 static void beyond ();
175 test_syntax_error (format, arg)
178 #if !defined (STANDALONE)
179 extern int interactive_shell;
180 extern char *get_name_for_error ();
181 if (interactive_shell == 0)
182 fprintf (stderr, "%s: ", get_name_for_error ());
184 fprintf (stderr, "%s: ", argv[0]);
185 fprintf (stderr, format, arg);
186 fprintf (stderr, "\n");
188 test_exit (SHELL_BOOLEAN (FALSE));
191 /* A wrapper for stat () which disallows pathnames that are empty strings
192 and handles /dev/fd emulation on systems that don't have it. */
194 test_stat (path, finfo)
203 if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
205 #if !defined (HAVE_DEV_FD)
207 if (isint (path + 8, &fd))
208 return (fstat ((int)fd, finfo));
215 /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
216 trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
217 On most systems, with the notable exception of linux, this is
218 effectively a no-op. */
220 strcpy (pbuf, DEV_FD_PREFIX);
221 strcat (pbuf, path + 8);
222 return (stat (pbuf, finfo));
223 #endif /* !HAVE_DEV_FD */
225 return (stat (path, finfo));
228 /* Do the same thing access(2) does, but use the effective uid and gid,
229 and don't make the mistake of telling root that any file is
232 test_eaccess (path, mode)
237 static int euid = -1;
239 if (test_stat (path, &st) < 0)
247 /* Root can read or write any file. */
251 /* Root can execute any file that has any one of the execute
253 if (st.st_mode & S_IXUGO)
257 if (st.st_uid == euid) /* owner */
259 else if (group_member (st.st_gid))
262 if (st.st_mode & mode)
268 #if defined (HAVE_GETGROUPS)
269 /* The number of groups that this user is a member of. */
270 static int ngroups, maxgroups;
271 static GETGROUPS_T *group_array = (GETGROUPS_T *)NULL;
272 #endif /* HAVE_GETGROUPS */
274 #if !defined (NOGROUP)
275 # define NOGROUP (gid_t) -1
278 #if defined (HAVE_GETGROUPS)
280 # if defined (NGROUPS_MAX)
281 # define getmaxgroups() NGROUPS_MAX
282 # else /* !NGROUPS_MAX */
283 # if defined (NGROUPS)
284 # define getmaxgroups() NGROUPS
285 # else /* !NGROUPS */
286 # define getmaxgroups() 64
287 # endif /* !NGROUPS */
288 # endif /* !NGROUPS_MAX */
290 #endif /* HAVE_GETGROUPS */
292 /* Return non-zero if GID is one that we have in our groups list. */
297 static gid_t pgid = (gid_t)NOGROUP;
298 static gid_t egid = (gid_t)NOGROUP;
299 #if defined (HAVE_GETGROUPS)
303 if (pgid == (gid_t)NOGROUP)
304 pgid = (gid_t) getgid ();
306 if (egid == (gid_t)NOGROUP)
307 egid = (gid_t) getegid ();
309 if (gid == pgid || gid == egid)
312 #if defined (HAVE_GETGROUPS)
313 /* getgroups () returns the number of elements that it was able to
314 place into the array. */
318 maxgroups = getmaxgroups ();
319 group_array = (GETGROUPS_T *)xrealloc (group_array, maxgroups * sizeof (GETGROUPS_T));
320 ngroups = getgroups (maxgroups, group_array);
323 /* In case of error, the user loses. */
327 /* Search through the list looking for GID. */
328 for (i = 0; i < ngroups; i++)
329 if (gid == (gid_t)group_array[i])
331 #endif /* HAVE_GETGROUPS */
336 /* Increment our position in the argument list. Check that we're not
337 past the end of the argument list. This check is supressed if the
338 argument is FALSE. Made a macro for efficiency. */
339 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
340 #define unary_advance() do { advance (1); ++pos; } while (0)
343 * beyond - call when we're beyond the end of the argument list (an
349 test_syntax_error ("argument expected", (char *)NULL);
352 /* Syntax error for when an integer argument was expected, but
353 something else was found. */
355 integer_expected_error (pch)
358 test_syntax_error ("%s: integer expression expected", pch);
361 #if defined (STANDALONE)
362 /* Return non-zero if the characters pointed to by STRING constitute a
363 valid number. Stuff the converted number into RESULT if RESULT is
364 a non-null pointer to a long. */
366 isint (string, result)
367 register char *string;
379 /* Skip leading whitespace characters. */
380 while (whitespace (*string))
386 /* We allow leading `-' or `+'. */
387 if (*string == '-' || *string == '+')
389 if (!digit (string[1]))
398 while (digit (*string))
401 value = (value * 10) + digit_value (*string);
405 /* Skip trailing whitespace, if any. */
406 while (whitespace (*string))
409 /* Error if not at end of string. */
421 #endif /* STANDALONE */
424 * term - parse a term and return 1 or 0 depending on whether the term
425 * evaluates to true or false, respectively.
428 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'p'|'r'|'s'|'u'|'w'|'x') filename
429 * '-'('G'|'L'|'O'|'S') filename
431 * '-'('z'|'n') string
433 * string ('!='|'='|'==') string
434 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
435 * file '-'(nt|ot|ef) file
438 * positive and negative integers
448 /* Deal with leading `not's. */
449 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
452 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
458 return (value ? !term() : term());
461 /* A paren-bracketed argument. */
462 if (argv[pos][0] == '(' && argv[pos][1] == '\0')
467 test_syntax_error ("`)' expected", (char *)NULL);
468 else if (argv[pos][0] != ')' || argv[pos][1])
469 test_syntax_error ("`)' expected, found %s", argv[pos]);
474 /* are there enough arguments left that this could be dyadic? */
475 if ((pos + 3 <= argc) && binop (argv[pos + 1]))
476 value = binary_operator ();
478 /* Might be a switch type argument */
479 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
481 if (unop (argv[pos][1]))
482 value = unary_operator ();
484 test_syntax_error ("%s: unary operator expected", argv[pos]);
488 value = argv[pos][0] != '\0';
500 struct stat st1, st2;
502 if (test_stat (s, &st1) < 0 || test_stat (t, &st2) < 0)
506 case OT: return (st1.st_mtime < st2.st_mtime);
507 case NT: return (st1.st_mtime > st2.st_mtime);
508 case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
520 if (isint (s, &l) == 0)
521 integer_expected_error (s);
522 if (isint (t, &r) == 0)
523 integer_expected_error (t);
526 case EQ: return (l == r);
527 case NE: return (l != r);
528 case LT: return (l < r);
529 case GT: return (l > r);
530 case LE: return (l <= r);
531 case GE: return (l >= r);
536 #if defined (PATTERN_MATCHING)
538 patcomp (string, pat, op)
544 m = fnmatch (pat, string, 0);
547 case EQ: return (m == 0);
548 case NE: return (m != 0);
551 #endif /* PATTERN_MATCHING */
560 if (w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0')))
562 value = STREQ (argv[pos], argv[pos + 2]);
566 if ((w[0] == '>' || w[0] == '<') && w[1] == '\0')
568 value = (w[0] == '>') ? strcmp (argv[pos], argv[pos + 2]) > 0
569 : strcmp (argv[pos], argv[pos + 2]) < 0;
573 #if defined (PATTERN_MATCHING)
574 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
576 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
581 if (w[0] == '!' && w[1] == '=' && w[2] == '\0')
583 value = STREQ (argv[pos], argv[pos + 2]) == 0;
588 if (w[0] != '-' || w[3] != '\0')
590 test_syntax_error ("%s: binary operator expected", w);
600 case 'n': value = filecomp (argv[pos], argv[pos + 2], NT); break;
601 case 'o': value = filecomp (argv[pos], argv[pos + 2], OT); break;
602 case 'l': value = arithcomp (argv[pos], argv[pos + 2], LT); break;
603 case 'g': value = arithcomp (argv[pos], argv[pos + 2], GT); break;
604 default: test_syntax_error ("-%s: binary operator expected", w);
607 else if (w[0] == 'e')
611 case 'q': value = arithcomp (argv[pos], argv[pos + 2], EQ); break;
612 case 'f': value = filecomp (argv[pos], argv[pos + 2], EF); break;
613 default: test_syntax_error ("-%s: binary operator expected", w);
616 else if (w[1] == 'e')
620 case 'n': value = arithcomp (argv[pos], argv[pos + 2], NE); break;
621 case 'g': value = arithcomp (argv[pos], argv[pos + 2], GE); break;
622 case 'l': value = arithcomp (argv[pos], argv[pos + 2], LE); break;
623 default: test_syntax_error ("-%s: binary operator expected", w);
627 test_syntax_error ("-%s: binary operator expected", w);
637 struct stat stat_buf;
639 switch (argv[pos][1])
644 /* All of the following unary operators use unary_advance (), which
645 checks to make sure that there is an argument, and then advances
646 pos right past it. This means that pos - 1 is the location of the
649 case 'a': /* file exists in the file system? */
652 return (test_stat (argv[pos - 1], &stat_buf) == 0);
654 case 'r': /* file is readable? */
656 return (EACCESS (argv[pos - 1], R_OK) == 0);
658 case 'w': /* File is writeable? */
660 return (EACCESS (argv[pos - 1], W_OK) == 0);
662 case 'x': /* File is executable? */
664 return (EACCESS (argv[pos - 1], X_OK) == 0);
666 case 'O': /* File is owned by you? */
668 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
669 (uid_t) geteuid () == (uid_t) stat_buf.st_uid);
671 case 'G': /* File is owned by your group? */
673 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
674 (gid_t) getegid () == (gid_t) stat_buf.st_gid);
676 case 'f': /* File is a file? */
678 if (test_stat (argv[pos - 1], &stat_buf) < 0)
681 /* -f is true if the given file exists and is a regular file. */
683 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
685 return (S_ISREG (stat_buf.st_mode));
688 case 'd': /* File is a directory? */
690 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
691 (S_ISDIR (stat_buf.st_mode)));
693 case 's': /* File has something in it? */
695 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
696 stat_buf.st_size > (off_t) 0);
698 case 'S': /* File is a socket? */
699 #if !defined (S_ISSOCK)
703 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
704 S_ISSOCK (stat_buf.st_mode));
705 #endif /* S_ISSOCK */
707 case 'c': /* File is character special? */
709 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
710 S_ISCHR (stat_buf.st_mode));
712 case 'b': /* File is block special? */
714 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
715 S_ISBLK (stat_buf.st_mode));
717 case 'p': /* File is a named pipe? */
722 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
723 S_ISFIFO (stat_buf.st_mode));
724 #endif /* S_ISFIFO */
726 case 'L': /* Same as -h */
727 case 'h': /* File is a symbolic link? */
729 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
732 return ((argv[pos - 1][0] != '\0') &&
733 (lstat (argv[pos - 1], &stat_buf) == 0) &&
734 S_ISLNK (stat_buf.st_mode));
735 #endif /* S_IFLNK && HAVE_LSTAT */
737 case 'u': /* File is setuid? */
739 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
740 (stat_buf.st_mode & S_ISUID) != 0);
742 case 'g': /* File is setgid? */
744 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
745 (stat_buf.st_mode & S_ISGID) != 0);
747 case 'k': /* File has sticky bit set? */
749 #if !defined (S_ISVTX)
750 /* This is not Posix, and is not defined on some Posix systems. */
753 return (test_stat (argv[pos - 1], &stat_buf) == 0 &&
754 (stat_buf.st_mode & S_ISVTX) != 0);
757 case 't': /* File fd is a terminal? fd defaults to stdout. */
759 if (pos < argc && isint (argv[pos], &r))
762 return (isatty ((int)r));
766 case 'n': /* True if arg has some length. */
768 return (argv[pos - 1][0] != '\0');
770 case 'z': /* True if arg has no length. */
772 return (argv[pos - 1][0] == '\0');
774 #if !defined (STANDALONE)
777 return (minus_o_option_value (argv[pos - 1]) == 1);
778 #endif /* !STANDALONE */
793 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
797 return (value && v2);
813 while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
817 return (value || v2);
833 return (FALSE ^ or ()); /* Same with this. */
836 /* Return TRUE if S is one of the test command's binary operators. */
843 if (s[0] == '=' && s[1] == '\0')
844 return (1); /* '=' */
845 else if (s[1] == '\0' && (s[0] == '<' || s[0] == '>')) /* string <, > */
847 else if (s[2] == '\0' && s[1] == '=' && (s[0] == '=' || s[0] == '!'))
848 return (1); /* `==' and `!=' */
849 #if defined (PATTERN_MATCHING)
850 else if (s[2] == '\0' && s[1] == '~' && (s[0] == '=' || s[0] == '!'))
853 else if (s[0] != '-' || s[2] == '\0' || s[3] != '\0')
869 else if (t[0] == 'e')
878 else if (t[1] == 'e')
893 /* Return non-zero if OP is one of the test command's unary operators. */
900 case 'a': case 'b': case 'c': case 'd': case 'e':
901 case 'f': case 'g': case 'h': case 'k': case 'n':
902 case 'p': case 'r': case 's': case 't': case 'u':
903 case 'w': case 'x': case 'z':
904 case 'G': case 'L': case 'O': case 'S':
905 #if !defined (STANDALONE)
916 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
917 return (argv[pos + 1][0] == '\0');
918 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
920 if (unop (argv[pos][1]))
921 return (unary_operator ());
923 test_syntax_error ("%s: unary operator expected", argv[pos]);
926 test_syntax_error ("%s: unary operator expected", argv[pos]);
931 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
933 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
940 if (binop (argv[pos+1]))
942 value = binary_operator ();
945 else if (ANDOR (argv[pos+1]))
947 if (argv[pos+1][1] == 'a')
948 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
950 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
953 else if (argv[pos][0] == '!' && !argv[pos][1])
956 value = !two_arguments ();
958 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
960 value = ONE_ARG_TEST(argv[pos+1]);
964 test_syntax_error ("%s: binary operator expected", argv[pos+1]);
969 /* This is an implementation of a Posix.2 proposal by David Korn. */
975 switch (argc - 1) /* one extra passed in */
983 value = ONE_ARG_TEST(argv[1]);
988 value = two_arguments ();
993 value = three_arguments ();
997 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
1000 value = !three_arguments ();
1024 #if !defined (STANDALONE)
1027 code = setjmp (test_exit_buf);
1030 return (test_error_return);
1031 #endif /* !STANDALONE */
1035 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
1040 test_exit (SHELL_BOOLEAN (FALSE));
1042 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
1043 test_syntax_error ("missing `]'", (char *)NULL);
1050 test_exit (SHELL_BOOLEAN (FALSE));
1053 value = posixtest ();
1056 test_syntax_error ("too many arguments", (char *)NULL);
1058 test_exit (SHELL_BOOLEAN (value));