1 /* Copyright (C) 1991, 1992, 1995, 1996 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
26 /* Execute FILE, searching in the `PATH' environment variable if it contains
27 no slashes, with arguments ARGV and environment from `environ'. */
35 void execute (const char *file
, char *const argv
[])
41 /* The file is accessible but it is not an executable file.
42 Invoke the shell to interpret it as a script. */
44 /* Count the arguments. */
49 /* Construct an argument list for the shell. */
51 char *new_argv
[argc
+ 1];
52 new_argv
[0] = (char *) _PATH_BSHELL
;
53 new_argv
[1] = (char *) file
;
56 new_argv
[argc
] = argv
[argc
- 1];
60 /* Execute the shell. */
61 execv (new_argv
[0], new_argv
);
66 if (strchr (file
, '/') != NULL
)
67 /* Don't search when it contains a slash. */
71 char *path
, *p
, *name
;
74 path
= getenv ("PATH");
77 /* There is no `PATH' in the environment.
78 The default search path is the current directory
79 followed by the path `confstr' returns for `_CS_PATH'. */
80 len
= confstr (_CS_PATH
, (char *) NULL
, 0);
81 path
= (char *) __alloca (1 + len
);
83 (void) confstr (_CS_PATH
, path
+ 1, len
);
86 len
= strlen (file
) + 1;
87 name
= __alloca (strlen (path
) + len
);
92 p
= strchr (path
, ':');
94 p
= strchr (path
, '\0');
97 /* Two adjacent colons, or a colon at the beginning or the end
98 of `PATH' means to search the current directory. */
99 (void) memcpy (name
, file
, len
);
102 /* Construct the pathname to try. */
103 (void) memcpy (name
, path
, p
- path
);
104 name
[p
- path
] = '/';
105 (void) memcpy (&name
[(p
- path
) + 1], file
, len
);
108 /* Try to execute this name. If it works, execv will not return. */
109 execute (name
, argv
);
114 /* Record the we got a `Permission denied' error. If we end
115 up finding no executable we can use, we want to diagnose
116 that we did find one but were denied access. */
119 /* Those errors indicate the file is missing or not executable
120 by us, in which case we want to just try the next path
125 /* Some other error means we found an executable file, but
126 something went wrong executing it; return the error to our
131 while (*p
++ != '\0');
134 /* We tried every element and none of them worked. */
137 /* At least one failure was due to permissions, so report that error. */
140 /* Return the error from the last attempt (probably ENOENT). */