projet-systeme-exploitation.../minishell.c

136 lines
3.4 KiB
C
Raw Normal View History

2021-04-20 16:12:44 +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"
int prompting = 0;
int job_id = 1;
list jobs;
2021-04-20 16:50:27 +00:00
void handler_print(int signal_num, siginfo_t *info)
{
if (contiens(&jobs, info->si_pid))
{
if (prompting)
{
printf("\n");
}
printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num));
supprimer(&jobs, info->si_pid);
job_id--;
}
2021-04-20 16:12:44 +00:00
}
2021-04-20 16:50:27 +00:00
int main(int argc, char *argv[])
{
2021-04-20 16:12:44 +00:00
2021-04-20 16:50:27 +00:00
extern int errno;
2021-04-20 16:12:44 +00:00
2021-04-20 16:50:27 +00:00
initialiser(&jobs);
2021-04-20 16:12:44 +00:00
2021-04-20 16:54:36 +00:00
// gestion de SIGCHLD
2021-04-20 16:50:27 +00:00
struct sigaction action;
action.sa_flags = SA_SIGINFO; //| SA_RESTART;
action.sa_handler = handler_print;
sigemptyset(&action.sa_mask);
sigaction(SIGCHLD, &action, NULL);
2021-04-20 16:12:44 +00:00
2021-04-20 16:54:36 +00:00
// variables de "pwd"
2021-04-20 16:50:27 +00:00
char initcd[256], currentcd[256];
getcwd(initcd, 256);
2021-04-20 16:12:44 +00:00
2021-04-20 16:54:36 +00:00
// loop principal
2021-04-20 16:50:27 +00:00
while (1)
{
getcwd(currentcd, 256);
2021-04-20 16:54:36 +00:00
2021-04-20 16:50:27 +00:00
printf("%s >>> ", currentcd);
2021-04-20 16:54:36 +00:00
2021-04-20 16:50:27 +00:00
prompting = 1;
struct cmdline *cmd = readcmd();
prompting = 0;
2021-04-20 16:12:44 +00:00
2021-04-20 16:50:27 +00:00
if (cmd == NULL || cmd->seq[0] == NULL)
{ // ligne vide, on skip
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // on quitte le shell
break;
}
else if (!strcmp(cmd->seq[0][0], "cd"))
{ // cd
if (cmd->seq[0][1] == NULL)
{ // vide
chdir(initcd);
}
else
{ // avec un path
chdir(cmd->seq[0][1]);
}
continue;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
2021-04-20 16:54:36 +00:00
{ // on affiche tous les process de fond
2021-04-20 16:50:27 +00:00
afficher(&jobs);
continue;
}
pid_t pidFils = fork();
2021-04-20 16:12:44 +00:00
2021-04-20 16:50:27 +00:00
if (pidFils == -1)
2021-04-20 16:54:36 +00:00
{ // si le fork échoue
2021-04-20 16:50:27 +00:00
fprintf(stderr, "ERROR: forking failed, %s\n", strerror(errno));
exit(errno);
2021-04-20 16:12:44 +00:00
}
2021-04-20 16:50:27 +00:00
if (pidFils == 0)
2021-04-20 16:54:36 +00:00
{ // instructions du fils
2021-04-20 16:50:27 +00:00
int test = execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
2021-04-20 16:54:36 +00:00
else
{ // instructions du père
2021-04-20 16:50:27 +00:00
if (cmd->backgrounded)
2021-04-20 16:54:36 +00:00
{ // on sauvegarde le pid du procces mis en fond
2021-04-20 16:50:27 +00:00
ajouter(&jobs, pidFils, job_id++);
}
else
{
jmp_buf env;
2021-04-20 16:54:36 +00:00
setjmp(env); // sauvegarde pour un jump
2021-04-20 16:50:27 +00:00
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)
2021-04-20 16:54:36 +00:00
{
longjmp(env, 1); // si interruption du wait, on jump
2021-04-20 16:50:27 +00:00
}
else
{
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno));
exit(errno);
}
}
if (codeTerm)
2021-04-20 16:54:36 +00:00
{ // si l'exec a échoué
2021-04-20 16:50:27 +00:00
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pidFils, codeTerm, strerror(codeTerm));
}
}
}
2021-04-20 16:12:44 +00:00
}
2021-04-20 16:50:27 +00:00
return EXIT_SUCCESS;
2021-04-20 16:12:44 +00:00
}