isblank() implementation.
[minix.git] / lib / libc / posix / _execvp.c
blobfecb08088334bd0847e6d97b079de500bca2591e
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 execve _execve
9 #define execvp _execvp
10 #define sbrk _sbrk
11 #define stat _stat
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/stat.h>
17 #include <lib.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, '/') != NULL || (path= getenv("PATH")) == NULL)
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;