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

224 lines
7.3 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>
2021-05-10 14:08:04 +00:00
#include <fcntl.h>
2021-04-20 16:12:44 +00:00
#include <errno.h>
#include <setjmp.h>
#include "readcmd.h"
#include "jobs.h"
2021-05-23 14:22:11 +00:00
#include "utils.h"
2021-04-20 16:12:44 +00:00
2021-04-21 14:18:53 +00:00
extern int errno;
2021-04-21 12:15:14 +00:00
struct cmdline *cmd;
2021-04-20 16:12:44 +00:00
2021-04-21 14:18:53 +00:00
list jobs;
2021-05-23 14:22:11 +00:00
int pid_fils;
int wait_code;
int builtin_code;
2021-04-21 14:18:53 +00:00
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
2021-05-10 14:08:04 +00:00
int file_in, file_out;
2021-05-23 09:58:39 +00:00
int pipes[256][2];
int sous_fils[256];
2021-04-21 14:18:53 +00:00
int main(int argc, char *argv[])
{
2021-04-20 16:50:27 +00:00
initialiser(&jobs);
2021-04-21 14:18:53 +00:00
getcwd(initcd, sizeof(initcd));
2021-04-20 16:12:44 +00:00
2021-04-21 13:12:19 +00:00
// gestion des signaux
2021-04-20 16:50:27 +00:00
struct sigaction action;
2021-04-20 20:19:58 +00:00
sigemptyset(&action.sa_mask);
2021-04-21 11:50:58 +00:00
action.sa_flags = SA_SIGINFO | SA_RESTART;
2021-04-21 14:18:53 +00:00
action.sa_handler = handler_sigchld;
2021-04-20 16:50:27 +00:00
sigaction(SIGCHLD, &action, NULL);
2021-04-21 14:18:53 +00:00
action.sa_handler = handler_sigint;
2021-04-20 20:19:58 +00:00
sigaction(SIGINT, &action, NULL);
2021-04-21 14:18:53 +00:00
action.sa_handler = handler_sigtstp;
2021-04-21 11:50:58 +00:00
sigaction(SIGTSTP, &action, NULL);
2021-04-20 16:12:44 +00:00
2021-04-24 15:32:54 +00:00
// main loop
2021-04-20 16:50:27 +00:00
while (1)
{
2021-04-21 14:18:53 +00:00
sigsetjmp(goto_prompt, 1);
2021-04-20 16:54:36 +00:00
2021-04-20 16:50:27 +00:00
prompting = 1;
2021-04-21 14:18:53 +00:00
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
2021-04-21 12:15:14 +00:00
cmd = readcmd();
2021-04-20 16:50:27 +00:00
prompting = 0;
2021-04-20 16:12:44 +00:00
2021-05-23 14:22:11 +00:00
builtin_code = builtin();
if (builtin_code == 1)
{
2021-04-20 16:50:27 +00:00
break;
}
2021-05-23 14:22:11 +00:00
else if (builtin_code == 2)
{
2021-04-24 17:53:12 +00:00
continue;
}
2021-05-23 14:22:11 +00:00
else if (builtin_code != 0)
{
fprintf(stderr, "ERROR: builtin command failed");
exit(1);
2021-04-24 15:32:54 +00:00
}
2021-04-20 16:50:27 +00:00
2021-05-23 09:58:39 +00:00
int nb_pipe = -1;
char ***cursor = cmd->seq;
while (*cursor)
2021-05-23 13:49:32 +00:00
{ // compter le nombre de commandes séparées par des pipes
2021-05-23 09:58:39 +00:00
cursor++;
nb_pipe++;
}
if (nb_pipe < 0)
{ // counting failed ?
fprintf(stderr, "ERROR: counting pipes failed");
2021-05-23 14:22:11 +00:00
exit(2);
2021-05-23 09:58:39 +00:00
}
if ((pid_fils = fork()) == -1)
2021-05-23 14:23:44 +00:00
{ // fork failed ?
2021-04-20 17:00:16 +00:00
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
2021-04-20 16:50:27 +00:00
exit(errno);
2021-04-20 16:12:44 +00:00
}
2021-04-21 14:18:53 +00:00
if (pid_fils == 0)
2021-04-20 16:54:36 +00:00
{ // instructions du fils
2021-04-24 15:32:54 +00:00
action.sa_handler = SIG_IGN;
2021-05-23 09:58:39 +00:00
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
2021-04-24 15:32:54 +00:00
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
2021-04-21 14:18:53 +00:00
2021-05-10 14:08:04 +00:00
if (cmd->in)
2021-05-23 09:58:39 +00:00
{ // "<"
2021-05-10 14:08:04 +00:00
file_in = open(cmd->in, O_RDONLY);
2021-05-23 13:49:32 +00:00
dup2(file_in, STDIN_FILENO);
2021-05-10 14:08:04 +00:00
}
if (cmd->out)
2021-05-23 09:58:39 +00:00
{ // ">"
2021-05-10 14:08:04 +00:00
file_out = open(cmd->out, O_CREAT | O_TRUNC | O_WRONLY, 0640);
2021-05-23 13:49:32 +00:00
dup2(file_out, STDOUT_FILENO);
2021-05-10 14:08:04 +00:00
}
2021-05-23 09:58:39 +00:00
if (nb_pipe > 0)
2021-05-23 13:49:32 +00:00
{ // "|"
2021-05-23 09:58:39 +00:00
for (int i = 0; i <= nb_pipe; i++)
2021-05-23 13:49:32 +00:00
{ // on créé itérativement nb_pipe fils et pipes
2021-05-23 09:58:39 +00:00
if (pipe(pipes[i]) < 0)
{ // pipe failed ?
fprintf(stderr, "ERROR: pipe error, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
if ((sous_fils[i] = fork()) < 0)
{ // fork failed ?
fprintf(stderr, "ERROR: fork error, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
else if (sous_fils[i] == 0)
{ // instructions des sous-fils
if (i == 0)
{ // premier sous-fils
close(pipes[0][0]);
}
else if (dup2(pipes[i - 1][0], STDIN_FILENO) == -1)
2021-05-23 14:23:44 +00:00
{ // dup2 failed ?
2021-05-23 09:58:39 +00:00
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
if (i == nb_pipe)
{ // dernier sous-fils
close(pipes[i][1]);
}
else if (dup2(pipes[i][1], STDOUT_FILENO) == -1)
2021-05-23 14:23:44 +00:00
{ // dup2 failed ?
2021-05-23 09:58:39 +00:00
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
for (int j = 0; j <= i; j++)
{ // on ferme les pipes non nécéssaires
if (j <= i - 2)
{ // on ferme tous les pipes que l'on utilise pas
close(pipes[j][0]);
close(pipes[j][1]);
}
else if (j == i - 1)
{ // on ferme l'écriture du pipe précédent
close(pipes[j][1]);
}
else if (j == i)
{ // on ferme la lecture de son propre pipe
close(pipes[j][0]);
}
}
execvp(cmd->seq[i][0], cmd->seq[i]);
fprintf(stderr, "ERROR: execvp failed, (%d) %s\n", errno, strerror(errno));
exit(errno); // si execvp échoue on exit avec une erreur
}
}
for (int i = 0; i <= nb_pipe; i++)
{ // on ferme tous les pipes pour le fils
close(pipes[i][0]);
close(pipes[i][1]);
}
for (int i = 0; i <= nb_pipe; i++)
{ // on attend chaque sous-fils
2021-05-23 13:49:32 +00:00
if (waitpid(sous_fils[i], &wait_code, 0) == -1)
2021-05-23 14:23:44 +00:00
{ // wait failed ?
2021-05-23 09:58:39 +00:00
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
exit(errno);
2021-05-23 13:49:32 +00:00
} // TODO: factoriser dans une fonction, cf dessous ?
2021-05-23 09:58:39 +00:00
if (wait_code)
2021-05-23 14:23:44 +00:00
{ // execvp failed ?
2021-05-23 09:58:39 +00:00
fprintf(stderr, "ERROR: child failed, (%d) %s\n", wait_code, strerror(wait_code));
}
}
exit(0); // on termine le fils
}
else
2021-05-23 13:49:32 +00:00
{ // pas de pipes dans la commande
2021-05-23 09:58:39 +00:00
execvp(cmd->seq[0][0], cmd->seq[0]);
fprintf(stderr, "ERROR: execvp failed, (%d) %s\n", errno, strerror(errno));
exit(errno); // si execvp échoue on exit avec une erreur
}
2021-04-20 16:50:27 +00:00
}
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-21 14:18:53 +00:00
{ // background
2021-04-24 15:32:54 +00:00
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d\n", id_fils, pid_fils);
2021-04-20 16:50:27 +00:00
}
else
2021-04-21 14:18:53 +00:00
{ // foreground
2021-05-23 13:49:32 +00:00
if (waitpid(pid_fils, &wait_code, 0) == -1)
2021-05-23 14:23:44 +00:00
{ // wait failed ?
2021-04-24 19:16:17 +00:00
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
exit(errno);
}
if (wait_code)
2021-05-23 14:23:44 +00:00
{ // execvp failed ?
2021-05-10 14:08:04 +00:00
fprintf(stderr, "ERROR: child failed, (%d) %s\n", wait_code, strerror(wait_code));
2021-04-24 19:16:17 +00:00
}
2021-04-20 16:50:27 +00:00
}
}
2021-04-20 16:12:44 +00:00
}
2021-05-23 13:49:32 +00:00
liberer(&jobs);
2021-04-20 16:50:27 +00:00
return EXIT_SUCCESS;
2021-05-23 14:22:39 +00:00
}