1 This file is command.def, from which is created command.c.
2 It implements the builtin "command" in Bash.
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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.
25 $FUNCTION command_builtin
26 $SHORT_DOC command [-pVv] [command [arg ...]]
27 Runs COMMAND with ARGS ignoring shell functions. If you have a shell
28 function called `ls', and you wish to call the command `ls', you can
29 say "command ls". If the -p option is given, a default value is used
30 for PATH that is guaranteed to find all of the standard utilities. If
31 the -V or -v option is given, a string is printed describing COMMAND.
32 The -V option produces a more verbose description.
35 #if defined (HAVE_STRING_H)
37 #else /* !HAVE_STRING_H */
39 #endif /* !HAVE_STRING_H */
42 #include "bashgetopt.h"
44 extern int subshell_environment;
46 static void restore_path ();
47 static char *get_standard_path ();
49 /* Run the commands mentioned in LIST without paying attention to shell
52 command_builtin (list)
55 int result, verbose = 0, use_standard_path = 0, opt;
58 reset_internal_getopt ();
59 while ((opt = internal_getopt (list, "pvV")) != -1)
64 use_standard_path = 1;
75 builtin_error ("usage: command [-pvV] [command [arg...]]");
82 return (EXECUTION_SUCCESS);
86 int found, any_found = 0;
91 found = describe_command (list->word->word, verbose, 0);
94 builtin_error ("%s: not found", list->word->word);
99 return (any_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
102 begin_unwind_frame ("command_builtin");
104 /* We don't want this to be reparsed (consider command echo 'foo &'), so
105 just make a simple_command structure and call execute_command with it. */
109 if (use_standard_path)
113 old_path = get_string_value ("PATH");
115 old_path = savestring (old_path);
117 old_path = savestring ("");
118 add_unwind_protect ((Function *)restore_path, old_path);
120 standard_path = get_standard_path ();
121 bind_variable ("PATH", standard_path);
122 free (standard_path);
124 command = make_bare_simple_command ();
125 command->value.Simple->words = (WORD_LIST *)copy_word_list (list);
126 command->value.Simple->redirects = (REDIRECT *)NULL;
127 command->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
128 command->value.Simple->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
129 /* If we're in a subshell, see if we can get away without forking
130 again, since we've already forked to run this builtin. */
131 if (subshell_environment)
133 command->flags |= CMD_NO_FORK;
134 command->value.Simple->flags |= CMD_NO_FORK;
136 add_unwind_protect ((char *)dispose_command, command);
137 result = execute_command (command);
140 run_unwind_frame ("command_builtin");
145 /* Restore the value of the $PATH variable after replacing it when
146 executing `command -p'. */
151 bind_variable ("PATH", var);
155 /* Return a value for PATH that is guaranteed to find all of the standard
156 utilities. This uses Posix.2 configuration variables, if present. It
157 uses a value defined in config.h as a last resort. */
161 #if defined (_CS_PATH) && !defined (hpux_7) && !defined (NetBSD)
165 len = (size_t)confstr (_CS_PATH, (char *)NULL, (size_t)0);
166 p = xmalloc ((int)len + 2);
168 confstr (_CS_PATH, p, len);
170 #else /* !_CSPATH || hpux_7 || NetBSD */
171 # if defined (CS_PATH)
172 return (savestring (CS_PATH));
174 return (savestring (STANDARD_UTILS_PATH));
175 # endif /* !CS_PATH */
176 #endif /* !_CS_PATH || hpux_7 */