TP-systeme-exploitation-cen.../TP1/fournitures/verif/miniminishell.c

54 lines
1 KiB
C
Raw Permalink Normal View History

2023-06-20 18:57:09 +00:00
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char *argv[]) {
int codeTerm;
pid_t pidFils, idFils;
char buf[31];
while (1) {
printf(">>> ");
int ret = scanf("%s", buf);
if (ret != 1) {
printf("\n");
break;
} else if (!strcmp("exit", buf)) { // on quitte le shell
break;
}
if (strcmp(buf, "")) { // si buf est vide on fait rien
pidFils = fork();
if (pidFils == -1) {
printf("ERROR fork\n");
exit(1);
}
if (pidFils == 0) {
execlp(buf, " ", NULL);
exit(2); // si execlp échoue on exit avec une erreur
} else {
idFils = wait(&codeTerm); // on attend la fin de l'exec de la commande
if (idFils == -1){
perror("wait ");
exit(3);
}
if (WEXITSTATUS(codeTerm) == 0) {
printf("SUCCES\n");
} else {
printf("ECHEC\n");
}
}
}
}
printf("Salut\n");
return EXIT_SUCCESS;
}