exec_file.c
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <err.h>
|
||||
|
||||
int execute_me(char *cmd, char **argv){
|
||||
|
||||
if (!cmd || !argv || !*argv) return 1;
|
||||
|
||||
int wstatus;
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid < 0) return -1;
|
||||
|
||||
// Child
|
||||
else if (pid == 0) {
|
||||
|
||||
if (cmd != *argv) exit(-1);
|
||||
else exit(execvp(cmd, argv));
|
||||
}
|
||||
|
||||
// Daddy
|
||||
else {
|
||||
|
||||
if(waitpid(pid, &wstatus, 0) == -1) return -1;
|
||||
|
||||
if(WIFEXITED(wstatus)) return WEXITSTATUS(wstatus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
int main(){
|
||||
char *argv[] = {"ls", "-l", NULL};
|
||||
int r1 = execute_me("ls", argv);
|
||||
printf("%d\n", r1);
|
||||
|
||||
int r2 = execute_me("wrong", argv);
|
||||
printf("%d\n", r2);
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user