]> sourceware.org Git - glibc.git/blob - posix/execvp.c
Thu Feb 8 18:55:27 1996 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / posix / execvp.c
1 /* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19 #include <unistd.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 /* Execute FILE, searching in the `PATH' environment variable if it contains
26 no slashes, with arguments ARGV and environment from `environ'. */
27 int
28 execvp (file, argv)
29 const char *file;
30 char *const argv[];
31 {
32 if (strchr (file, '/') != NULL)
33 /* Don't search when it contains a slash. */
34 return execv (file, argv);
35 else
36 {
37 char *path, *p, *name;
38 size_t len;
39
40 path = getenv ("PATH");
41 if (path == NULL)
42 {
43 /* There is no `PATH' in the environment.
44 The default search path is the current directory
45 followed by the path `confstr' returns for `_CS_PATH'. */
46 len = confstr (_CS_PATH, (char *) NULL, 0);
47 path = (char *) __alloca (1 + len);
48 path[0] = ':';
49 (void) confstr (_CS_PATH, path + 1, len);
50 }
51
52 len = strlen (file) + 1;
53 name = __alloca (strlen (path) + len);
54 p = path;
55 do
56 {
57 path = p;
58 p = strchr (path, ':');
59 if (p == NULL)
60 p = strchr (path, '\0');
61
62 if (p == path)
63 /* Two adjacent colons, or a colon at the beginning or the end
64 of `PATH' means to search the current directory. */
65 (void) memcpy (name, file, len);
66 else
67 {
68 /* Construct the pathname to try. */
69 (void) memcpy (name, path, p - path);
70 name[p - path] = '/';
71 (void) memcpy (&name[(p - path) + 1], file, len);
72 }
73
74 /* Try to execute this name. If it works, execv will not return. */
75 execv (name, argv);
76 if (errno != ENOENT && errno != EACCES)
77 /* Those errors indicate the file is missing or not executable
78 by us, in which case we want to just try the next path
79 directory. Some other error means we found an executable
80 file, but something went wrong executing it; return the
81 error to our caller. */
82 return -1;
83 }
84 while (*p++ != '\0');
85 }
86
87 /* We tried every element and none of them worked.
88 Return the error from the last attempt (probably ENOENT). */
89 return -1;
90 }
This page took 0.045126 seconds and 5 git commands to generate.