2021-04-21 13:12:19 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include "readcmd.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
|
2021-04-21 13:19:23 +00:00
|
|
|
extern int errno;
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
struct cmdline *cmd;
|
2021-04-21 13:19:23 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
int pid_fils;
|
2021-04-21 13:19:23 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
int job_id = 0;
|
|
|
|
list jobs;
|
2021-04-21 13:19:23 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
int prompting = 0;
|
|
|
|
jmp_buf goto_prompt;
|
|
|
|
|
2021-04-21 13:19:23 +00:00
|
|
|
char initcd[256], currentcd[256];
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
void handler_sigchld(int signal_num, siginfo_t *info)
|
|
|
|
{
|
|
|
|
cell *job = trouver(&jobs, info->si_pid);
|
|
|
|
if (job)
|
|
|
|
{
|
|
|
|
if (prompting)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:19:23 +00:00
|
|
|
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
|
2021-04-21 13:12:19 +00:00
|
|
|
supprimer(&jobs, job->pid);
|
|
|
|
job_id--;
|
|
|
|
|
|
|
|
if (prompting)
|
|
|
|
{
|
|
|
|
siglongjmp(goto_prompt, signal_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:19:23 +00:00
|
|
|
|
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
initialiser(&jobs);
|
2021-04-21 13:19:23 +00:00
|
|
|
getcwd(initcd, sizeof(initcd));
|
2021-04-21 13:12:19 +00:00
|
|
|
|
|
|
|
// gestion des signaux
|
|
|
|
struct sigaction action;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = SA_SIGINFO | SA_RESTART;
|
|
|
|
action.sa_handler = handler_sigchld;
|
|
|
|
sigaction(SIGCHLD, &action, NULL);
|
|
|
|
|
|
|
|
// loop principal
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
sigsetjmp(goto_prompt, 1);
|
|
|
|
|
|
|
|
prompting = 1;
|
2021-04-21 13:19:23 +00:00
|
|
|
getcwd(currentcd, sizeof(currentcd));
|
|
|
|
printf("%s >>> ", currentcd);
|
2021-04-21 13:12:19 +00:00
|
|
|
cmd = readcmd();
|
|
|
|
prompting = 0;
|
|
|
|
|
|
|
|
if (cmd == NULL)
|
|
|
|
{ // EOF
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmd->seq[0] == NULL)
|
|
|
|
{ // empty
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "exit"))
|
|
|
|
{ // "exit"
|
|
|
|
break;
|
|
|
|
}
|
2021-04-21 13:19:23 +00:00
|
|
|
else if (!strcmp(cmd->seq[0][0], "jobs"))
|
|
|
|
{ // "jobs"
|
|
|
|
afficher(&jobs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(cmd->seq[0][0], "cd"))
|
|
|
|
{ // "cd"
|
|
|
|
int ret = 0;
|
|
|
|
if (cmd->seq[0][1] == NULL)
|
|
|
|
{ // no path
|
|
|
|
ret = chdir(initcd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // with path
|
|
|
|
ret = chdir(cmd->seq[0][1]);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{ // wrong path
|
|
|
|
fprintf(stderr, "ERROR: cd failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-21 13:12:19 +00:00
|
|
|
|
|
|
|
pid_fils = fork();
|
|
|
|
if (pid_fils == -1)
|
|
|
|
{ // fork fail ?
|
|
|
|
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid_fils == 0)
|
|
|
|
{ // instructions du fils
|
|
|
|
if (cmd->backgrounded)
|
|
|
|
{ // background
|
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
|
|
|
|
}
|
|
|
|
execvp(cmd->seq[0][0], cmd->seq[0]);
|
|
|
|
exit(errno); // si execlp échoue on exit avec une erreur
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // instructions du père
|
|
|
|
if (cmd->backgrounded)
|
|
|
|
{ // background
|
|
|
|
ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 0);
|
|
|
|
printf("[%d] %d\n", job_id, pid_fils);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // foreground
|
|
|
|
int wait_code;
|
|
|
|
pid_t id_fils = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
|
2021-04-21 13:19:23 +00:00
|
|
|
|
2021-04-21 13:12:19 +00:00
|
|
|
if (id_fils == -1)
|
|
|
|
{ // wait fail ?
|
|
|
|
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_code)
|
|
|
|
{ // execvp fail ?
|
|
|
|
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pid_fils, wait_code, strerror(wait_code));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|