This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2026-04-14 12:54:40 +02:00

43 lines
871 B
C

#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_me2(char *file, char **args){
if(!file || !args || !*args) return 1;
int wstatus;
pid_t pid = fork();
if (pid < 0) return -1;
// Child
else if (pid == 0) {
if(file != *args) exit(-1);
exit(execlp(file, args[0], args[1], (char *)NULL));
}
// 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};
execute_me2("ls", argv); // should be the same as doing "ls -l" in the shell
//char *argv[] = {"ls", "-l", NULL};
int i = execute_me2("wrong", argv); // should return 255
}*/