#include #include #include #include #include #include #include #include "readcmd.h" #include "processlist.h" #define BUFFERSIZE 255 process * list; int sortie = 0; int errno; int cpt = 0; void suivi_fils (int sig) { int etat_fils, pid_fils; do { pid_fils = waitpid(-1, &etat_fils, WNOHANG | WUNTRACED | WCONTINUED); if ((pid_fils == -1) && (errno != ECHILD)) { perror("waitpid"); exit(EXIT_FAILURE); } else if (pid_fils > 0) { if (WIFSTOPPED(etat_fils)) { list = setStatus(list,pid_fils,0); } else if (WIFCONTINUED(etat_fils)) { list = setStatus(list,pid_fils,1); } else if (WIFEXITED(etat_fils)) { list = supprimer(list, pid_fils); printf("suppression"); } else if (WIFSIGNALED(etat_fils)) { //if(WSTOPSIG(etat_fils) == ) } } } while (pid_fils > 0); } char * copyCommandText(char ** cmd){ char * cmdtxt = malloc(BUFFERSIZE * sizeof(char)); int x=0; while( cmd[x]!= NULL){ strcat(cmdtxt, cmd[x]); x++; } return cmdtxt; } void executerCommande(char ** commande, int backgrounded){ if(strcmp(commande[0], "cd") == 0){ char *s = malloc(sizeof(char)*250); getcwd(s, 255); // cas de la commande cd sans argument if(commande[1] == NULL){ chdir(getenv("HOME")); } // cas de cd avec chemin relatif ou absolu else if(chdir(commande[1]) == 0 || chdir(strcat(s, commande[1])) == 0){} // cas ou le repertoire renseigné n'existe pas else{ printf("Repertoire non accessible %s", s); } free(s); } else if(strcmp(commande[0], "list") == 0){ printf("%-12s%-12s%-12s%s\n", "ID", "PID", "STATUS", "COMMANDE"); afficherProcess(list); } else if(strcmp(commande[0], "stop") == 0){ if(commande[1] == NULL) printf("Utilisation : stop id\n"); else{ int pid = getPID(list, atoi(commande[1])); //printf("%d", pid); kill(pid , SIGSTOP); setStatus(list, pid, 0); } } else if(strcmp(commande[0], "exit") == 0){ sortie = 1; } else{ int status; int pid = fork(); list = inserer(list, cpt, pid, 1, copyCommandText(commande)); cpt++; if(pid == 0) { //sigset_t ens_signaux; //sigemptyset(&ens_signaux); //sigaddset(&ens_signaux, SIGINT); //sigaddset(&ens_signaux, SIGUSR1); //printf("Masquage de SIGINT et SIGUSR1\n\n"); //sigprocmask(SIG_SETMASK, &ens_signaux, NULL); execvp(commande[0],commande); printf("Erreur sur la commande\n"); exit(3); } else if (pid == -1) { printf("Une erreur est sur venu au niveau du fork\n"); exit(2); } if(backgrounded < 1){ int ret = waitpid(pid, &status, 0); list = supprimer(list, pid); if(ret == -1){ perror("Erreur sur le wait\n"); exit(3); } } } } int main() { //signal(SIGINT, handler_SIG); //signal(20, handler_SIG); struct cmdline *cmd; int i; while(sortie != 1){ printf("minishell$ "); cmd = readcmd(); i = 0; while(cmd->seq[i] != NULL){ if(cmd->backgrounded == NULL) executerCommande(cmd->seq[i], 0); else executerCommande(cmd->seq[i], 1); i++; } } }