spawn.c (919B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <sys/types.h> 3 #include <sys/wait.h> 4 5 #include <errno.h> 6 #include <stdarg.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 10 #include "util.h" 11 12 int 13 spawnvp(char *dir, char *file, char *argv[]) 14 { 15 pid_t pid; 16 int status, r; 17 18 pid = fork(); 19 switch (pid) { 20 case -1: 21 return -1; 22 case 0: 23 if (dir != NULL && chdir(dir) == -1) 24 exit(1); 25 execvp(file, argv); 26 _exit(1); 27 default: 28 while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) 29 continue; 30 if (r == -1) 31 return -1; 32 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) 33 return -1; 34 } 35 return 0; 36 } 37 38 int 39 spawnlp(char *dir, char *file, char *argv0, ...) 40 { 41 char *argv[NR_ARGS]; 42 va_list ap; 43 int argc; 44 45 va_start(ap, argv0); 46 argv[0] = argv0; 47 for (argc = 1; (argv[argc] = va_arg(ap, char *)) != (void *)0; argc++) 48 ; 49 argv[argc] = NULL; 50 va_end(ap); 51 return spawnvp(dir, file, argv); 52 }