1 /* Copyright (C) 1991,92,1995-99,2002,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 /* The file is accessible but it is not an executable file. Invoke
28 the shell to interpret it as a script. */
31 script_execute (const char *file
, char *const argv
[])
33 /* Count the arguments. */
38 /* Construct an argument list for the shell. */
40 char *new_argv
[argc
+ 1];
41 new_argv
[0] = (char *) _PATH_BSHELL
;
42 new_argv
[1] = (char *) file
;
45 new_argv
[argc
] = argv
[argc
- 1];
49 /* Execute the shell. */
50 __execve (new_argv
[0], new_argv
, __environ
);
55 /* Execute FILE, searching in the `PATH' environment variable if it contains
56 no slashes, with arguments ARGV and environment from `environ'. */
64 /* We check the simple case first. */
69 if (strchr (file
, '/') != NULL
)
71 /* Don't search when it contains a slash. */
72 __execve (file
, argv
, __environ
);
75 script_execute (file
, argv
);
80 char *path
, *p
, *name
;
84 path
= getenv ("PATH");
87 /* There is no `PATH' in the environment.
88 The default search path is the current directory
89 followed by the path `confstr' returns for `_CS_PATH'. */
90 len
= confstr (_CS_PATH
, (char *) NULL
, 0);
91 path
= (char *) __alloca (1 + len
);
93 (void) confstr (_CS_PATH
, path
+ 1, len
);
96 len
= strlen (file
) + 1;
97 pathlen
= strlen (path
);
98 name
= __alloca (pathlen
+ len
+ 1);
99 /* Copy the file name at the top. */
100 name
= (char *) memcpy (name
+ pathlen
+ 1, file
, len
);
101 /* And add the slash. */
110 p
= __strchrnul (path
, ':');
113 /* Two adjacent colons, or a colon at the beginning or the end
114 of `PATH' means to search the current directory. */
117 startp
= (char *) memcpy (name
- (p
- path
), path
, p
- path
);
119 /* Try to execute this name. If it works, execv will not return. */
120 __execve (startp
, argv
, __environ
);
122 if (errno
== ENOEXEC
)
123 script_execute (startp
, argv
);
128 /* Record the we got a `Permission denied' error. If we end
129 up finding no executable we can use, we want to diagnose
130 that we did find one but were denied access. */
135 /* Those errors indicate the file is missing or not executable
136 by us, in which case we want to just try the next path
140 /* Some strange filesystems like AFS return even
141 stranger error numbers. They cannot reasonably mean
142 anything else so ignore those, too. */
146 /* Some other error means we found an executable file, but
147 something went wrong executing it; return the error to our
152 while (*p
++ != '\0');
154 /* We tried every element and none of them worked. */
156 /* At least one failure was due to permissions, so report that
158 __set_errno (EACCES
);
161 /* Return the error from the last attempt (probably ENOENT). */
164 libc_hidden_def (execvp
)