*** empty log message ***
[glibc/history.git] / posix / execvp.c
blob01ae0d10a9c24e168d7d6d9f0799b29c07531085
1 /* Copyright (C) 1991, 1992 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. */
19 #include <ansidecl.h>
20 #include <unistd.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <limits.h>
26 #include <sys/types.h>
28 #ifndef HAVE_GNU_LD
29 #define __environ environ
30 #endif
32 /* Execute FILE, searching in the `PATH' environment variable if it contains
33 no slashes, with arguments ARGV and environment from `environ'. */
34 int
35 DEFUN(execvp, (file, argv), CONST char *file AND char *CONST argv[])
37 if (strchr (file, '/') == NULL)
39 char *path, *p;
40 struct stat st;
41 size_t len;
42 uid_t uid;
43 gid_t gid;
44 int ngroups;
45 gid_t groups[NGROUPS_MAX];
46 char *name;
48 path = getenv ("PATH");
49 if (path == NULL)
51 /* There is no `PATH' in the environment.
52 The default search path is the current directory
53 followed by the path `confstr' returns for `_CS_PATH'. */
54 len = confstr (_CS_PATH, (char *) NULL, 0);
55 path = (char *) __alloca (1 + len);
56 path[0] = ':';
57 (void) confstr (_CS_PATH, path + 1, len);
60 len = strlen (file) + 1;
61 name = __alloca (strlen (path) + len);
62 uid = geteuid ();
63 gid = getegid ();
64 ngroups = getgroups (sizeof (groups) / sizeof (groups[0]), groups);
65 p = path;
68 path = p;
69 p = strchr (path, ':');
70 if (p == NULL)
71 p = strchr (path, '\0');
73 if (p == path)
74 /* Two adjacent colons, or a colon at the beginning or the end
75 of `PATH' means to search the current directory. */
76 (void) memcpy (name, file, len);
77 else
79 /* Construct the pathname to try. */
80 (void) memcpy (name, path, p - path);
81 name[p - path] = '/';
82 (void) memcpy (&name[(p - path) + 1], file, len);
84 if (stat (name, &st) == 0 && S_ISREG (st.st_mode))
86 int bit = S_IXOTH;
87 if (st.st_uid == uid)
88 bit = S_IXUSR;
89 else if (st.st_gid == gid)
90 bit = S_IXGRP;
91 else
93 register int i;
94 for (i = 0; i < ngroups; ++i)
95 if (st.st_gid == groups[i])
97 bit = S_IXGRP;
98 break;
101 if (st.st_mode & bit)
103 file = name;
104 break;
108 while (*p++ != '\0');
111 return __execve (file, argv, __environ);