fixed more binutils issues (newer gcc/libc)
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / posix / execvp.c
blob70743163dc455ba60ccc419ca8d821bdffc134f0
1 /* execvp.c */
3 /* This and the other exec*.c files in this directory require
4 the target to provide the _execve syscall. */
6 #include <_ansi.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <ctype.h>
14 #define PATH_DELIM ':'
17 * Copy string, until c or <nul> is encountered.
18 * NUL-terminate the destination string (s1).
21 static char *
22 _DEFUN (strccpy, (s1, s2, c),
23 char *s1 _AND
24 char *s2 _AND
25 char c)
27 char *dest = s1;
29 while (*s2 && *s2 != c)
30 *s1++ = *s2++;
31 *s1 = 0;
33 return dest;
36 int
37 _DEFUN (execvp, (file, argv),
38 _CONST char *file _AND
39 char * _CONST argv[])
41 char *path = getenv ("PATH");
42 char buf[MAXNAMLEN];
44 /* If $PATH doesn't exist, just pass FILE on unchanged. */
45 if (!path)
46 return execv (file, argv);
48 /* If FILE contains a directory, don't search $PATH. */
49 if (strchr (file, '/')
51 return execv (file, argv);
53 while (*path)
55 strccpy (buf, path, PATH_DELIM);
56 /* An empty entry means the current directory. */
57 if (*buf != 0 && buf[strlen(buf) - 1] != '/')
58 strcat (buf, "/");
59 strcat (buf, file);
60 if (execv (buf, argv) == -1 && errno != ENOENT)
61 return -1;
62 while (*path && *path != PATH_DELIM)
63 path++;
64 if (*path == PATH_DELIM)
65 path++; /* skip over delim */
68 return -1;