1 | /* execvp() - execute with PATH search and prepared arguments
|
---|
2 | * Author: Kees J. Bot
|
---|
3 | * 21 Jan 1994
|
---|
4 | */
|
---|
5 |
|
---|
6 | #define _MINIX_SOURCE
|
---|
7 |
|
---|
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>
|
---|
18 |
|
---|
19 | extern char * const **_penviron; /* The default environment. */
|
---|
20 |
|
---|
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.
|
---|
27 | */
|
---|
28 | {
|
---|
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. */
|
---|
35 |
|
---|
36 | if (strchr(file, '/') != nil || (path= getenv("PATH")) == nil)
|
---|
37 | path= "";
|
---|
38 |
|
---|
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);
|
---|
42 |
|
---|
43 | /* Claim space. */
|
---|
44 | if ((full= (char *) sbrk(full_size)) == (char *) -1) {
|
---|
45 | errno= E2BIG;
|
---|
46 | return -1;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* For each directory in the path... */
|
---|
50 | do {
|
---|
51 | f= full;
|
---|
52 | while (*path != 0 && *path != ':') *f++= *path++;
|
---|
53 |
|
---|
54 | if (f > full) *f++= '/';
|
---|
55 |
|
---|
56 | strcpy(f, file);
|
---|
57 |
|
---|
58 | /* Stat first, small speed-up, better for ptrace. */
|
---|
59 | if (stat(full, &sb) == -1) continue;
|
---|
60 |
|
---|
61 | (void) execve(full, argv, *_penviron);
|
---|
62 |
|
---|
63 | /* Prefer more interesting errno values then "not there". */
|
---|
64 | if (errno != ENOENT) err= errno;
|
---|
65 |
|
---|
66 | /* Continue only on some errors. */
|
---|
67 | if (err != ENOENT && err != EACCES) break;
|
---|
68 | } while (*path++ != 0);
|
---|
69 |
|
---|
70 | (void) sbrk(-full_size);
|
---|
71 | errno= err;
|
---|
72 | return -1;
|
---|
73 | }
|
---|