Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / builtins / test.def
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, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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 (trueness) or 1 (falseness) 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     -b FILE        True if file is block special.
35     -c FILE        True if file is character special.
36     -d FILE        True if file is a directory.
37     -e FILE        True if file exists.
38     -f FILE        True if file exists and is a regular file.
39     -g FILE        True if file is set-group-id.
40     -h FILE        True if file is a symbolic link.  Use "-L".
41     -L FILE        True if file is a symbolic link.
42     -k FILE        True if file has its "sticky" bit set.
43     -p FILE        True if file is a named pipe.
44     -r FILE        True if file is readable by you.
45     -s FILE        True if file is not empty.
46     -S FILE        True if file is a socket.
47     -t FD          True if FD is opened on a terminal.
48     -u FILE        True if the file is set-user-id.
49     -w FILE        True if the file is writable by you.
50     -x FILE        True if the file is executable by you.
51     -O FILE        True if the file is effectively owned by you.
52     -G FILE        True if the file is effectively owned by your group.
53
54   FILE1 -nt FILE2  True if file1 is newer than (according to
55                    modification date) file2.
56
57   FILE1 -ot FILE2  True if file1 is older than file2.
58
59   FILE1 -ef FILE2  True if file1 is a hard link to file2.
60
61 String operators:
62
63     -z STRING      True if string is empty.
64
65     -n STRING
66  or STRING         True if string is not empty.
67
68     STRING1 = STRING2
69                    True if the strings are equal.
70     STRING1 != STRING2
71                    True if the strings are not equal.
72
73 Other operators:
74
75     ! EXPR         True if expr is false.
76     EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
77     EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
78
79     arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
80                    -lt, -le, -gt, or -ge.
81
82 Arithmetic binary operators return true if ARG1 is equal, not-equal,
83 less-than, less-than-or-equal, greater-than, or greater-than-or-equal
84 than ARG2.
85 $END
86
87 $BUILTIN [
88 $DOCNAME test_bracket
89 $FUNCTION test_builtin
90 $SHORT_DOC [ arg... ]
91 This is a synonym for the "test" shell builtin, excepting that the
92 last argument must be literally `]', to match the `[' which invoked
93 the test.
94 $END
95
96 #if defined (HAVE_STRING_H)
97 #  include <string.h>
98 #else /* !HAVE_STRING_H */
99 #  include <strings.h>
100 #endif /* !HAVE_STRING_H */
101
102 #include "../shell.h"
103 extern char *this_command_name;
104
105 /* TEST/[ builtin. */
106 int
107 test_builtin (list)
108      WORD_LIST *list;
109 {
110   char **argv;
111   int argc, result;
112   WORD_LIST *t = list;
113
114   /* We let Matthew Bradburn and Kevin Braunsdorf's code do the
115      actual test command.  So turn the list of args into an array
116      of strings, since that is what his code wants. */
117   if (!list)
118     {
119       if (this_command_name[0] == '[' && !this_command_name[1])
120         builtin_error ("missing `]'");
121
122       return (EXECUTION_FAILURE);
123     }
124
125   /* Get the length of the argument list. */
126   for (argc = 0; t; t = t->next, argc++);
127
128   /* Account for argv[0] being a command name.  This makes our life easier. */
129   argc++;
130   argv = (char **)xmalloc ((1 + argc) * sizeof (char *));
131   argv[argc] = (char *)NULL;
132
133   /* this_command_name is the name of the command that invoked this
134      function.  So you can't call test_builtin () directly from
135      within this code, there are too many things to worry about. */
136   argv[0] = savestring (this_command_name);
137
138   for (t = list, argc = 1; t; t = t->next, argc++)
139     argv[argc] = savestring (t->word->word);
140
141   result = test_command (argc, argv);
142   free_array (argv);
143   return (result);
144 }