Update.
[glibc.git] / posix / execvp.c
blob2eda603c399a100749f8dcd01bd965318beb89f7
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1997 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <unistd.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <paths.h>
27 static void
28 internal_function
29 execute (const char *file, char *const argv[])
31 execv (file, argv);
33 if (errno == ENOEXEC)
35 /* The file is accessible but it is not an executable file.
36 Invoke the shell to interpret it as a script. */
38 /* Count the arguments. */
39 int argc = 0;
40 while (argv[argc++])
43 /* Construct an argument list for the shell. */
45 char *new_argv[argc + 1];
46 new_argv[0] = (char *) _PATH_BSHELL;
47 new_argv[1] = (char *) file;
48 while (argc > 1)
50 new_argv[argc] = argv[argc - 1];
51 --argc;
54 /* Execute the shell. */
55 execv (new_argv[0], new_argv);
61 /* Execute FILE, searching in the `PATH' environment variable if it contains
62 no slashes, with arguments ARGV and environment from `environ'. */
63 int
64 execvp (file, argv)
65 const char *file;
66 char *const argv[];
68 int got_eacces = 0;
70 if (strchr (file, '/') != NULL)
71 /* Don't search when it contains a slash. */
72 execute (file, argv);
73 else
75 char *path, *p, *name;
76 size_t len;
78 path = getenv ("PATH");
79 if (path == NULL)
81 /* There is no `PATH' in the environment.
82 The default search path is the current directory
83 followed by the path `confstr' returns for `_CS_PATH'. */
84 len = confstr (_CS_PATH, (char *) NULL, 0);
85 path = (char *) __alloca (1 + len);
86 path[0] = ':';
87 (void) confstr (_CS_PATH, path + 1, len);
90 len = strlen (file) + 1;
91 name = __alloca (strlen (path) + len);
92 p = path;
95 path = p;
96 p = strchr (path, ':');
97 if (p == NULL)
98 p = strchr (path, '\0');
100 if (p == path)
101 /* Two adjacent colons, or a colon at the beginning or the end
102 of `PATH' means to search the current directory. */
103 (void) memcpy (name, file, len);
104 else
106 /* Construct the pathname to try. */
107 char *tmp = __mempcpy (name, path, p - path);
108 *tmp++ = '/';
109 (void) __mempcpy (tmp, file, len);
112 /* Try to execute this name. If it works, execv will not return. */
113 execute (name, argv);
115 switch (errno)
117 case EACCES:
118 /* Record the we got a `Permission denied' error. If we end
119 up finding no executable we can use, we want to diagnose
120 that we did find one but were denied access. */
121 got_eacces = 1;
122 case ENOENT:
123 case ESTALE:
124 /* Those errors indicate the file is missing or not executable
125 by us, in which case we want to just try the next path
126 directory. */
127 break;
129 default:
130 /* Some other error means we found an executable file, but
131 something went wrong executing it; return the error to our
132 caller. */
133 return -1;
136 while (*p++ != '\0');
139 /* We tried every element and none of them worked. */
141 if (got_eacces)
142 /* At least one failure was due to permissions, so report that error. */
143 __set_errno (EACCES);
145 /* Return the error from the last attempt (probably ENOENT). */
146 return -1;