]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/execvp.c
update from main archive
[thirdparty/glibc.git] / posix / execvp.c
CommitLineData
6a032d81 1/* Copyright (C) 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
28f540f4
RM
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
28f540f4
RM
19#include <unistd.h>
20#include <stdarg.h>
21#include <stdlib.h>
22#include <string.h>
60092701 23#include <errno.h>
6a032d81 24#include <paths.h>
28f540f4
RM
25
26/* Execute FILE, searching in the `PATH' environment variable if it contains
27 no slashes, with arguments ARGV and environment from `environ'. */
28int
c4bf5a3e
RM
29execvp (file, argv)
30 const char *file;
31 char *const argv[];
28f540f4 32{
a1c46301
RM
33 int got_eacces = 0;
34
6a032d81
RM
35 void execute (const char *file, char *const argv[])
36 {
37 execv (file, argv);
38
39 if (errno == ENOEXEC)
40 {
41 /* The file is accessible but it is not an executable file.
42 Invoke the shell to interpret it as a script. */
43
6a032d81 44 /* Count the arguments. */
a1c46301
RM
45 int argc = 0;
46 while (argv[argc++])
47 ;
6a032d81
RM
48
49 /* Construct an argument list for the shell. */
a1c46301
RM
50 {
51 char *new_argv[argc + 1];
52 new_argv[0] = (char *) _PATH_BSHELL;
53 new_argv[1] = (char *) file;
54 while (argc > 1)
55 {
56 new_argv[argc] = argv[argc - 1];
57 --argc;
58 }
59
60 /* Execute the shell. */
61 execv (new_argv[0], new_argv);
62 }
6a032d81
RM
63 }
64 }
65
c4bf5a3e
RM
66 if (strchr (file, '/') != NULL)
67 /* Don't search when it contains a slash. */
6a032d81 68 execute (file, argv);
c4bf5a3e 69 else
28f540f4 70 {
c4bf5a3e 71 char *path, *p, *name;
28f540f4 72 size_t len;
28f540f4
RM
73
74 path = getenv ("PATH");
75 if (path == NULL)
76 {
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);
82 path[0] = ':';
83 (void) confstr (_CS_PATH, path + 1, len);
84 }
85
86 len = strlen (file) + 1;
87 name = __alloca (strlen (path) + len);
28f540f4
RM
88 p = path;
89 do
90 {
91 path = p;
92 p = strchr (path, ':');
93 if (p == NULL)
94 p = strchr (path, '\0');
95
96 if (p == path)
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);
100 else
101 {
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);
106 }
c4bf5a3e
RM
107
108 /* Try to execute this name. If it works, execv will not return. */
6a032d81
RM
109 execute (name, argv);
110
111 switch (errno)
112 {
6a032d81 113 case EACCES:
a1c46301
RM
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. */
117 got_eacces = 1;
118 case ENOENT:
6a032d81
RM
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
121 directory. */
122 break;
123
124 default:
125 /* Some other error means we found an executable file, but
126 something went wrong executing it; return the error to our
127 caller. */
128 return -1;
129 }
28f540f4
RM
130 }
131 while (*p++ != '\0');
132 }
133
a1c46301
RM
134 /* We tried every element and none of them worked. */
135
136 if (got_eacces)
137 /* At least one failure was due to permissions, so report that error. */
c4029823 138 __set_errno (EACCES);
a1c46301
RM
139
140 /* Return the error from the last attempt (probably ENOENT). */
c4bf5a3e 141 return -1;
28f540f4 142}