Simplistic example
Code:
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int mysystem(const char *command) {
int status = -1;
const char *cmd = command ? command : "exit 0";
pid_t pid = fork();
switch (pid) {
case -1: // Couldn't create process
break;
case 0: // Child process
execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
break;
default: // Parent process
wait(&status);
}
return command ? status : !status;
}
void printstatus(int status) {
if (status == -1)
fputs("Error when creating child process or retrieving its exit status.\n\n", stderr);
else
printf("Child process exited with status %d\n\n", status);
}
int main(void) {
int status = mysystem(NULL);
printf("Checking if command processor exists: %d\n\n", status);
status = mysystem("ls /");
printstatus(status);
status = mysystem("ls /tmp");
printstatus(status);
return 0;
}