Last change
on this file was
444,
checked in by satin@…, 6 years ago
|
add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc
|
File size:
1.3 KB
|
Line | |
---|
1 | #ifndef _NO_EXECVE |
---|
2 | |
---|
3 | /* execvp.c */ |
---|
4 | |
---|
5 | /* This and the other exec*.c files in this directory require |
---|
6 | the target to provide the _execve syscall. */ |
---|
7 | |
---|
8 | #include <_ansi.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <dirent.h> |
---|
12 | #include <string.h> |
---|
13 | #include <errno.h> |
---|
14 | #include <ctype.h> |
---|
15 | |
---|
16 | #define PATH_DELIM ':' |
---|
17 | |
---|
18 | /* |
---|
19 | * Copy string, until c or <nul> is encountered. |
---|
20 | * NUL-terminate the destination string (s1). |
---|
21 | */ |
---|
22 | |
---|
23 | static char * |
---|
24 | strccpy (char *s1, |
---|
25 | char *s2, |
---|
26 | char c) |
---|
27 | { |
---|
28 | char *dest = s1; |
---|
29 | |
---|
30 | while (*s2 && *s2 != c) |
---|
31 | *s1++ = *s2++; |
---|
32 | *s1 = 0; |
---|
33 | |
---|
34 | return dest; |
---|
35 | } |
---|
36 | |
---|
37 | int |
---|
38 | execvp (const char *file, |
---|
39 | char * const argv[]) |
---|
40 | { |
---|
41 | char *path = getenv ("PATH"); |
---|
42 | char buf[MAXNAMLEN]; |
---|
43 | |
---|
44 | /* If $PATH doesn't exist, just pass FILE on unchanged. */ |
---|
45 | if (!path) |
---|
46 | return execv (file, argv); |
---|
47 | |
---|
48 | /* If FILE contains a directory, don't search $PATH. */ |
---|
49 | if (strchr (file, '/') |
---|
50 | ) |
---|
51 | return execv (file, argv); |
---|
52 | |
---|
53 | while (*path) |
---|
54 | { |
---|
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 */ |
---|
66 | } |
---|
67 | |
---|
68 | return -1; |
---|
69 | } |
---|
70 | |
---|
71 | #endif /* !_NO_EXECVE */ |
---|
Note: See
TracBrowser
for help on using the repository browser.