. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / posix / _execvp.c
blob14b768b1d714b4076efbee3f360ea646ff2ab1b8
1 /* execvp() - execute with PATH search and prepared arguments
2 * Author: Kees J. Bot
3 * 21 Jan 1994
4 */
6 #define _MINIX_SOURCE
8 #define nil 0
9 #define execve _execve
10 #define execvp _execvp
11 #define sbrk _sbrk
12 #define stat _stat
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/stat.h>
19 extern char * const **_penviron; /* The default environment. */
21 int execvp(const char *file, char * const *argv)
22 /* Execute the file with a path search on $PATH, just like the shell. The
23 * search continues on the errors ENOENT (not there), and EACCES (file not
24 * executable or leading directories protected.)
25 * Unlike other execvp implementations there is no default path, and no shell
26 * is started for scripts. One is supposed to define $PATH, and use #!/bin/sh.
29 struct stat sb;
30 const char *path; /* $PATH */
31 char *full; /* Full name to try. */
32 char *f;
33 size_t full_size;
34 int err= ENOENT; /* Error return on failure. */
36 if (strchr(file, '/') != nil || (path= getenv("PATH")) == nil)
37 path= "";
39 /* Compute the maximum length the full name may have, and align. */
40 full_size= strlen(path) + 1 + strlen(file) + 1 + sizeof(char *) - 1;
41 full_size&= ~(sizeof(char *) - 1);
43 /* Claim space. */
44 if ((full= (char *) sbrk(full_size)) == (char *) -1) {
45 errno= E2BIG;
46 return -1;
49 /* For each directory in the path... */
50 do {
51 f= full;
52 while (*path != 0 && *path != ':') *f++= *path++;
54 if (f > full) *f++= '/';
56 strcpy(f, file);
58 /* Stat first, small speed-up, better for ptrace. */
59 if (stat(full, &sb) == -1) continue;
61 (void) execve(full, argv, *_penviron);
63 /* Prefer more interesting errno values then "not there". */
64 if (errno != ENOENT) err= errno;
66 /* Continue only on some errors. */
67 if (err != ENOENT && err != EACCES) break;
68 } while (*path++ != 0);
70 (void) sbrk(-full_size);
71 errno= err;
72 return -1;