exec.c (1185B)
1 #include <unistd.h> 2 #include <stdlib.h> 3 #include <errno.h> 4 #include <sys/stat.h> 5 #include <string.h> 6 #include <stdio.h> 7 #include "syscall.h" 8 9 int execl(const char *path, const char *arg, ...) 10 { 11 return execv(path, (char *const*)&arg); 12 } 13 14 int execv(const char *path, char *const arg[]) 15 { 16 return execve(path, arg, environ); 17 } 18 19 int execve(const char *path, char *const arg[], char *const env[]) 20 { 21 return syscall(VXSYSEXEC, (int)path, (int)arg, (int)env, 0, 0); 22 } 23 24 int execlp(const char *path, const char *arg, ...) 25 { 26 return execvp(path, (char*const*)&arg); 27 } 28 29 int execvp(const char *file, char *const arg[]) 30 { 31 struct stat st; 32 33 if(file[0] == '/' || (file[0] == '.' && file[1] == '/')) 34 return execv(file, arg); 35 36 char buf[1024]; 37 char *path = getenv("PATH"); 38 if(path == NULL){ 39 errno = ENOENT; 40 return -1; 41 } 42 char *p, *ep; 43 int n; 44 for(p=path; p; p=ep){ 45 ep = strchr(p, ':'); 46 if(ep) 47 n = ep++ - p; 48 else 49 n = strlen(p); 50 if(n+1+strlen(file)+1 > sizeof buf) 51 continue; 52 if(n == 0) 53 strcpy(buf, file); 54 else{ 55 memmove(buf, p, n); 56 buf[n] = '/'; 57 strcpy(buf+n+1, file); 58 } 59 if(stat(buf, &st) >= 0) 60 return execv(buf, arg); 61 } 62 errno = ENOENT; 63 return -1; 64 }