This commit is contained in:
Laureηt 2021-04-20 18:54:36 +02:00
parent 3295e0df03
commit 004bc4ec4d

View file

@ -36,19 +36,24 @@ int main(int argc, char *argv[])
initialiser(&jobs);
// gestion de SIGCHLD
struct sigaction action;
action.sa_flags = SA_SIGINFO; //| SA_RESTART;
action.sa_handler = handler_print;
sigemptyset(&action.sa_mask);
sigaction(SIGCHLD, &action, NULL);
// variables de "pwd"
char initcd[256], currentcd[256];
getcwd(initcd, 256);
// loop principal
while (1)
{
getcwd(currentcd, 256);
printf("%s >>> ", currentcd);
prompting = 1;
struct cmdline *cmd = readcmd();
prompting = 0;
@ -74,7 +79,7 @@ int main(int argc, char *argv[])
continue;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{
{ // on affiche tous les process de fond
afficher(&jobs);
continue;
}
@ -82,35 +87,35 @@ int main(int argc, char *argv[])
pid_t pidFils = fork();
if (pidFils == -1)
{
{ // si le fork échoue
fprintf(stderr, "ERROR: forking failed, %s\n", strerror(errno));
exit(errno);
}
if (pidFils == 0)
{ // fils
{ // instructions du fils
int test = execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else // père
{
else
{ // instructions du père
if (cmd->backgrounded)
{
{ // on sauvegarde le pid du procces mis en fond
ajouter(&jobs, pidFils, job_id++);
}
else
{
jmp_buf env;
setjmp(env);
setjmp(env); // sauvegarde pour un jump
int codeTerm;
pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils
if (idFils == -1)
{ // si le wait fail
if (errno == 4)
{ // todo : peut mieux faire ?
longjmp(env, 1);
{
longjmp(env, 1); // si interruption du wait, on jump
}
else
{
@ -120,7 +125,7 @@ int main(int argc, char *argv[])
}
if (codeTerm)
{
{ // si l'exec a échoué
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pidFils, codeTerm, strerror(codeTerm));
}
}