1 | This file is test.def, from which is created test.c.
|
---|
2 | It implements the builtin "test" in Bash.
|
---|
3 |
|
---|
4 | Copyright (C) 1987-2002 Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This file is part of GNU Bash, the Bourne Again SHell.
|
---|
7 |
|
---|
8 | Bash is free software; you can redistribute it and/or modify it under
|
---|
9 | the terms of the GNU General Public License as published by the Free
|
---|
10 | Software Foundation; either version 2, or (at your option) any later
|
---|
11 | version.
|
---|
12 |
|
---|
13 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
---|
16 | for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License along
|
---|
19 | with Bash; see the file COPYING. If not, write to the Free Software
|
---|
20 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
---|
21 |
|
---|
22 | $PRODUCES test.c
|
---|
23 |
|
---|
24 | $BUILTIN test
|
---|
25 | $FUNCTION test_builtin
|
---|
26 | $SHORT_DOC test [expr]
|
---|
27 | Exits with a status of 0 (true) or 1 (false) depending on
|
---|
28 | the evaluation of EXPR. Expressions may be unary or binary. Unary
|
---|
29 | expressions are often used to examine the status of a file. There
|
---|
30 | are string operators as well, and numeric comparison operators.
|
---|
31 |
|
---|
32 | File operators:
|
---|
33 |
|
---|
34 | -a FILE True if file exists.
|
---|
35 | -b FILE True if file is block special.
|
---|
36 | -c FILE True if file is character special.
|
---|
37 | -d FILE True if file is a directory.
|
---|
38 | -e FILE True if file exists.
|
---|
39 | -f FILE True if file exists and is a regular file.
|
---|
40 | -g FILE True if file is set-group-id.
|
---|
41 | -h FILE True if file is a symbolic link.
|
---|
42 | -L FILE True if file is a symbolic link.
|
---|
43 | -k FILE True if file has its `sticky' bit set.
|
---|
44 | -p FILE True if file is a named pipe.
|
---|
45 | -r FILE True if file is readable by you.
|
---|
46 | -s FILE True if file exists and is not empty.
|
---|
47 | -S FILE True if file is a socket.
|
---|
48 | -t FD True if FD is opened on a terminal.
|
---|
49 | -u FILE True if the file is set-user-id.
|
---|
50 | -w FILE True if the file is writable by you.
|
---|
51 | -x FILE True if the file is executable by you.
|
---|
52 | -O FILE True if the file is effectively owned by you.
|
---|
53 | -G FILE True if the file is effectively owned by your group.
|
---|
54 | -N FILE True if the file has been modified since it was last read.
|
---|
55 |
|
---|
56 | FILE1 -nt FILE2 True if file1 is newer than file2 (according to
|
---|
57 | modification date).
|
---|
58 |
|
---|
59 | FILE1 -ot FILE2 True if file1 is older than file2.
|
---|
60 |
|
---|
61 | FILE1 -ef FILE2 True if file1 is a hard link to file2.
|
---|
62 |
|
---|
63 | String operators:
|
---|
64 |
|
---|
65 | -z STRING True if string is empty.
|
---|
66 |
|
---|
67 | -n STRING
|
---|
68 | STRING True if string is not empty.
|
---|
69 |
|
---|
70 | STRING1 = STRING2
|
---|
71 | True if the strings are equal.
|
---|
72 | STRING1 != STRING2
|
---|
73 | True if the strings are not equal.
|
---|
74 | STRING1 < STRING2
|
---|
75 | True if STRING1 sorts before STRING2 lexicographically.
|
---|
76 | STRING1 > STRING2
|
---|
77 | True if STRING1 sorts after STRING2 lexicographically.
|
---|
78 |
|
---|
79 | Other operators:
|
---|
80 |
|
---|
81 | -o OPTION True if the shell option OPTION is enabled.
|
---|
82 | ! EXPR True if expr is false.
|
---|
83 | EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
|
---|
84 | EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
|
---|
85 |
|
---|
86 | arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
|
---|
87 | -lt, -le, -gt, or -ge.
|
---|
88 |
|
---|
89 | Arithmetic binary operators return true if ARG1 is equal, not-equal,
|
---|
90 | less-than, less-than-or-equal, greater-than, or greater-than-or-equal
|
---|
91 | than ARG2.
|
---|
92 | $END
|
---|
93 |
|
---|
94 | $BUILTIN [
|
---|
95 | $DOCNAME test_bracket
|
---|
96 | $FUNCTION test_builtin
|
---|
97 | $SHORT_DOC [ arg... ]
|
---|
98 | This is a synonym for the "test" builtin, but the last
|
---|
99 | argument must be a literal `]', to match the opening `['.
|
---|
100 | $END
|
---|
101 |
|
---|
102 | #include <config.h>
|
---|
103 |
|
---|
104 | #if defined (HAVE_UNISTD_H)
|
---|
105 | # ifdef _MINIX
|
---|
106 | # include <sys/types.h>
|
---|
107 | # endif
|
---|
108 | # include <unistd.h>
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #include "../bashansi.h"
|
---|
112 |
|
---|
113 | #include "../shell.h"
|
---|
114 | #include "../test.h"
|
---|
115 | #include "common.h"
|
---|
116 |
|
---|
117 | extern char *this_command_name;
|
---|
118 |
|
---|
119 | /* TEST/[ builtin. */
|
---|
120 | int
|
---|
121 | test_builtin (list)
|
---|
122 | WORD_LIST *list;
|
---|
123 | {
|
---|
124 | char **argv;
|
---|
125 | int argc, result;
|
---|
126 |
|
---|
127 | /* We let Matthew Bradburn and Kevin Braunsdorf's code do the
|
---|
128 | actual test command. So turn the list of args into an array
|
---|
129 | of strings, since that is what their code wants. */
|
---|
130 | if (list == 0)
|
---|
131 | {
|
---|
132 | if (this_command_name[0] == '[' && !this_command_name[1])
|
---|
133 | {
|
---|
134 | builtin_error ("missing `]'");
|
---|
135 | return (EX_BADUSAGE);
|
---|
136 | }
|
---|
137 |
|
---|
138 | return (EXECUTION_FAILURE);
|
---|
139 | }
|
---|
140 |
|
---|
141 | argv = make_builtin_argv (list, &argc);
|
---|
142 | result = test_command (argc, argv);
|
---|
143 | free ((char *)argv);
|
---|
144 |
|
---|
145 | return (result);
|
---|
146 | }
|
---|