functionnel mais spaghet un peu

This commit is contained in:
Laureηt 2021-04-24 17:32:54 +02:00
parent 7c086ac01a
commit 6dfbc2f055
21 changed files with 1238 additions and 87 deletions

View file

@ -3,6 +3,7 @@
"signal.h": "c",
"readcmd.h": "c",
"stdlib.h": "c",
"wait.h": "c"
"wait.h": "c",
"*.bak": "c"
}
}

BIN
Q1 Executable file

Binary file not shown.

67
Q1.c Normal file
View file

@ -0,0 +1,67 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
int main(int argc, char *argv[])
{
// loop principal
while (1)
{
printf(">>> ");
cmd = readcmd();
if (cmd == NULL)
{ // EOF
break;
}
else if (cmd->seq[0] == NULL)
{ // empty
continue;
}
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
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else
{ // instructions du père
int wait_code;
pid_t id = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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;
}

71
Q3.c Normal file
View file

@ -0,0 +1,71 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
int main(int argc, char *argv[])
{
// loop principal
while (1)
{
printf(">>> ");
cmd = readcmd();
if (cmd == NULL)
{ // EOF
break;
}
else if (cmd->seq[0] == NULL)
{ // empty
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // "exit"
break;
}
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
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else
{ // instructions du père
int wait_code;
pid_t id = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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;
}

BIN
Q4 Executable file

Binary file not shown.

93
Q4.c Normal file
View file

@ -0,0 +1,93 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
char initcd[256], currentcd[256];
int main(int argc, char *argv[])
{
getcwd(initcd, sizeof(initcd));
// loop principal
while (1)
{
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
cmd = readcmd();
if (cmd == NULL)
{ // EOF
break;
}
else if (cmd->seq[0] == NULL)
{ // empty
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // "exit"
break;
}
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;
}
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
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else
{ // instructions du père
int wait_code;
pid_t id = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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;
}

BIN
Q5 Executable file

Binary file not shown.

115
Q5.c Normal file
View file

@ -0,0 +1,115 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
void handler_sigchld(int sig_num)
{
if (prompting)
{
printf("\n");
siglongjmp(goto_prompt, sig_num);
}
}
int main(int argc, char *argv[])
{
getcwd(initcd, sizeof(initcd));
// 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;
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
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;
}
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;
}
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
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
int wait_code;
pid_t id = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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;
}

BIN
Q6 Executable file

Binary file not shown.

204
Q6.c Normal file
View file

@ -0,0 +1,204 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils, state_fils;
list jobs;
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
void handler_sigchld(int sig_num)
{
pid_fils = waitpid(-1, &state_fils, WNOHANG | WUNTRACED | WCONTINUED);
cell *job = trouver(&jobs, pid_fils);
if (prompting)
{
printf("\n");
}
if (WIFSTOPPED(state_fils))
{
printf("[%d] %d suspended: %s\n", job->id, job->pid, job->cmd);
}
else if (WIFCONTINUED(state_fils))
{
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
//attendre(job->pid);
}
else if (WIFEXITED(state_fils))
{
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
}
// else if (WIFSIGNALED(state_fils))
// {
// /* traiter signal */
// }
if (prompting)
{
siglongjmp(goto_prompt, sig_num);
}
}
void handler_sigint(int sig_num)
{
printf("\n");
siglongjmp(goto_prompt, sig_num);
}
void attendre(int pid)
{
int wait_code;
pid_t id = waitpid(pid, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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, wait_code, strerror(wait_code));
}
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
getcwd(initcd, sizeof(initcd));
// 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);
action.sa_handler = handler_sigint;
sigaction(SIGINT, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
// loop principal
while (1)
{
sigsetjmp(goto_prompt, 1);
prompting = 1;
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
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;
}
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;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{ // "bg"
kill(pid_fils, SIGCONT);
continue;
}
else if (!strcmp(cmd->seq[0][0], "fg"))
{ // "fg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
fprintf(stderr, "ERROR: fg id error\n");
continue;
}
else
{ // id specified
job = &(jobs[atoi(cmd->seq[0][1]) - 1]);
}
kill(job->pid, SIGCONT);
continue;
}
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
action.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP
sigaction(SIGCONT, &action, NULL); // on default SIGCONT
if (cmd->backgrounded)
{ // background
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
}
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
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d\n", id_fils, pid_fils);
}
else
{ // foreground
attendre(pid_fils);
}
}
}
return EXIT_SUCCESS;
}

BIN
jobs

Binary file not shown.

62
jobs.c
View file

@ -3,9 +3,8 @@
#include <string.h>
#include "jobs.h"
void ajouter(list *l_ptr, int pid, int id, char **seq, int state)
int ajouter(list *l_ptr, int pid, char **seq)
{
cell *new_cell = malloc(sizeof(*new_cell));
char *cmd;
@ -19,26 +18,45 @@ void ajouter(list *l_ptr, int pid, int id, char **seq, int state)
strcat(cmd, " ");
}
new_cell->id = 1;
new_cell->pid = pid;
new_cell->id = id;
new_cell->cmd = cmd;
new_cell->state = state;
new_cell->next = NULL;
if (*l_ptr == NULL)
cell *cursor = *l_ptr;
if (cursor == NULL)
{
new_cell->next = NULL;
*l_ptr = new_cell;
}
else if (cursor->id > 1)
{
new_cell->next = cursor;
*l_ptr = new_cell;
}
else
{
new_cell->next = *l_ptr;
while (cursor->next != NULL)
{
new_cell->id++;
if (new_cell->id < cursor->next->id)
{
new_cell->next = cursor->next;
cursor->next = new_cell;
break;
}
cursor = cursor->next;
}
if (cursor->next == NULL)
{
new_cell->id++;
cursor->next = new_cell;
}
}
*l_ptr = new_cell;
return new_cell->id;
}
void supprimer(list *l_ptr, int pid)
{
cell *cursor = *l_ptr;
if (cursor->pid == pid)
@ -68,12 +86,10 @@ void supprimer(list *l_ptr, int pid)
void afficher(list *l_ptr)
{
cell *cursor = *l_ptr;
while (cursor != NULL)
{
printf("[%d] %d\n", cursor->id, cursor->pid);
printf("[%d] %d : %s\n", cursor->id, cursor->pid, cursor->cmd);
cursor = cursor->next;
}
}
@ -85,8 +101,8 @@ void initialiser(list *l_ptr)
void liberer(list *l_ptr)
{
cell* cursor2free;
cell* cursor = *l_ptr;
cell *cursor2free;
cell *cursor = *l_ptr;
while (cursor != NULL)
{
cursor2free = cursor;
@ -111,5 +127,21 @@ cell *trouver(list *l_ptr, int pid)
cursor = cursor->next;
}
return NULL; // erreur
}
cell *trouver_id(list *l_ptr, int id)
{
cell *cursor = *l_ptr;
while (cursor != NULL)
{
if (cursor->id == id)
{
return cursor;
}
cursor = cursor->next;
}
return NULL; // erreur
}

10
jobs.h
View file

@ -8,16 +8,18 @@ struct cell
int pid;
char *cmd;
cell *next;
int state;
};
typedef cell *list;
void ajouter(cell **list, int pid, int id, char **cmd, int state);
void supprimer(cell **list, int pid);
void afficher(cell **list);
void initialiser(list *list);
void liberer(list *list);
void afficher(cell **list);
int ajouter(cell **list, int pid, char **cmd);
void supprimer(cell **list, int pid);
cell *trouver(list *l_ptr, int pid);
cell *trouver_id(list *l_ptr, int id);
#endif

83
list.c Normal file
View file

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#define INIT_LIMIT 8
void init(list *l_ptr)
{
l_ptr->limit = INIT_LIMIT;
l_ptr->jobs = malloc(l_ptr->limit * sizeof(job));
if (l_ptr->jobs == NULL)
{
printf(stderr, "ERROR: malloc failed\n");
exit(1);
}
}
job add(list *l_ptr, int pid, char **cmd, int state)
{
job new_job;
new_job.pid = pid;
new_job.cmd = cmd;
new_job.used = 1;
for (int i = 0; i < l_ptr->size; i++)
{
if (!l_ptr->jobs[i].used)
{
l_ptr->jobs[i] = new_job;
return new_job;
}
}
l_ptr->limit = l_ptr->limit * 2;
l_ptr->jobs = realloc(l_ptr->jobs, l_ptr->limit * sizeof(job));
if (l_ptr->jobs == NULL)
{
printf(stderr, "ERROR: realloc failed\n");
exit(1);
}
l_ptr->jobs[l_ptr->limit / 2] = new_job;
return new_job;
}
int is_empty(list l_ptr)
{
return l_ptr.size == 0;
}
job remove(list *l_ptr, int pid)
{
if (is_empty(*l_ptr))
{
printf(stderr, "ERROR: jobs is empty, cannot remove\n");
exit(1);
}
for (int i = 0; i < l_ptr->size; i++)
{
if (l_ptr->jobs[i].pid == pid)
{
job job = l_ptr->jobs[i];
job.used = 0;
return l_ptr->jobs[i];
}
}
}
job top(list l_ptr)
{
return l_ptr.jobs[l_ptr.size - 1];
}
job get(list l_ptr, int index)
{
return l_ptr.jobs[index];
}
void free(list *l_ptr)
{
free(l_ptr->jobs);
l_ptr->jobs = NULL;
}

25
list.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef __LIST_H
#define __LIST_H
typedef struct job
{
int pid;
char *cmd;
int used;
} job;
typedef struct list {
job *jobs;
int size;
int limit;
} list;
void init(list *list);
void free(list *list);
void print(list *list);
job add(list *list, int pid, char **cmd, int state);
job remove(list *list, int pid);
#endif

BIN
minishell

Binary file not shown.

207
minishell copy.c Normal file
View file

@ -0,0 +1,207 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
list jobs;
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
void handler_sigchld(int sig_num, siginfo_t *info)
{
cell *job = trouver(&jobs, info->si_pid);
if (job)
{
if (prompting)
{
printf("\n");
}
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
if (prompting)
{
siglongjmp(goto_prompt, sig_num);
}
}
}
void handler_sigint(int sig_num)
{
printf("\n");
siglongjmp(goto_prompt, sig_num);
}
void handler_sigtstp(int sig_num)
{
if (!prompting)
{
kill(pid_fils, SIGTSTP);
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d suspended\n", id_fils, pid_fils);
siglongjmp(goto_prompt, sig_num);
}
else
{
handler_sigint(sig_num);
}
}
void attendre(int pid)
{
int wait_code;
pid_t id = waitpid(pid, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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, wait_code, strerror(wait_code));
}
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
getcwd(initcd, sizeof(initcd));
// 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);
action.sa_handler = handler_sigint;
sigaction(SIGINT, &action, NULL);
action.sa_handler = handler_sigtstp;
sigaction(SIGTSTP, &action, NULL);
// loop principal
while (1)
{
sigsetjmp(goto_prompt, 1);
prompting = 1;
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
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;
}
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;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{ // "bg"
kill(pid_fils, SIGCONT);
continue;
}
else if (!strcmp(cmd->seq[0][0], "fg"))
{ // "fg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
fprintf(stderr, "ERROR: fg id error");
continue;
}
else
{ // id specified
job = &(jobs[atoi(cmd->seq[0][1]) - 1]);
}
kill(job->pid, SIGCONT);
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
supprimer(&job, job->pid);
attendre(job->pid);
continue;
}
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
action.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP
sigaction(SIGCONT, &action, NULL); // on default SIGCONT
if (cmd->backgrounded)
{ // background
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
}
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
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d\n", id_fils, pid_fils);
}
else
{ // foreground
attendre(pid_fils);
}
}
}
return EXIT_SUCCESS;
}

View file

@ -15,9 +15,8 @@ extern int errno;
struct cmdline *cmd;
int pid_fils;
int pid_fils, wait_code;
int job_id = 0;
list jobs;
int prompting = 0;
@ -25,62 +24,86 @@ jmp_buf goto_prompt;
char initcd[256], currentcd[256];
void handler_sigchld(int sig_num, siginfo_t *info)
void handler_sigchld(int sig_num)
{
cell *job = trouver(&jobs, info->si_pid);
if (job)
do
{
if (prompting)
{
printf("\n");
pid_fils = waitpid(-1, &wait_code, WNOHANG | WUNTRACED | WCONTINUED);
if ((pid_fils == -1) && (errno != ECHILD))
{ // wait fail ?
fprintf(stderr, "ERROR: waiting for child failed, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
job_id--;
if (prompting)
cell *job = trouver(&jobs, pid_fils);
if (job != NULL)
{
siglongjmp(goto_prompt, sig_num);
if (prompting)
{
printf("\n");
}
if (WIFSTOPPED(wait_code))
{
printf("[%d] %d suspended: %s\n", job->id, job->pid, job->cmd);
}
else if (WIFCONTINUED(wait_code))
{
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
}
else if (WIFEXITED(wait_code))
{
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
}
if (prompting)
{
siglongjmp(goto_prompt, sig_num);
}
}
}
} while (pid_fils > 0);
}
void handler_sigint(int sig_num)
{
printf("\n");
if (!prompting)
{
kill(pid_fils, SIGKILL);
pause();
}
siglongjmp(goto_prompt, sig_num);
}
void handler_sigtstp(int sig_num)
{
printf("\n");
if (!prompting)
{
kill(pid_fils, SIGTSTP);
ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 1);
printf("[%d] %d suspended\n", job_id, pid_fils);
siglongjmp(goto_prompt, sig_num);
}
else
{
handler_sigint(sig_num);
ajouter(&jobs, pid_fils, *(cmd->seq));
kill(pid_fils, SIGSTOP);
pause();
}
siglongjmp(goto_prompt, sig_num);
}
void handler_sigcont(int sig_num)
void attendre(int pid)
{
pid_t id = waitpid(pid, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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, wait_code, strerror(wait_code));
}
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
@ -97,7 +120,7 @@ int main(int argc, char *argv[])
action.sa_handler = handler_sigtstp;
sigaction(SIGTSTP, &action, NULL);
// loop principal
// main loop
while (1)
{
sigsetjmp(goto_prompt, 1);
@ -120,11 +143,6 @@ int main(int argc, char *argv[])
{ // "exit"
break;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "cd"))
{ // "cd"
int ret = 0;
@ -142,6 +160,49 @@ int main(int argc, char *argv[])
}
continue;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{ // "bg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
fprintf(stderr, "ERROR: fg id error\n");
continue;
}
else
{ // id specified
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
}
kill(job->pid, SIGCONT);
pause();
continue;
}
else if (!strcmp(cmd->seq[0][0], "fg"))
{ // "fg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
fprintf(stderr, "ERROR: fg id error\n");
continue;
}
else
{ // id specified
job = trouver_id(&jobs, atoi(cmd->seq[0][1]));
}
kill(job->pid, SIGCONT);
pause();
attendre(job->pid);
continue;
}
else if (!strcmp(cmd->seq[0][0], "pid"))
{ // "pid"
printf("pid=%d\n", getpid());
continue;
}
pid_fils = fork();
if (pid_fils == -1)
@ -152,18 +213,10 @@ int main(int argc, char *argv[])
if (pid_fils == 0)
{ // instructions du fils
action.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP
sigaction(SIGCONT, &action, NULL); // on default SIGCONT
action.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
if (cmd->backgrounded)
{ // background
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
}
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
@ -171,24 +224,12 @@ int main(int argc, char *argv[])
{ // 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);
int id_fils = ajouter(&jobs, pid_fils, *(cmd->seq));
printf("[%d] %d\n", id_fils, 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
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));
}
attendre(pid_fils);
}
}
}

210
minishell2.c Normal file
View file

@ -0,0 +1,210 @@
#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"
extern int errno;
struct cmdline *cmd;
int pid_fils;
int job_id = 0;
list jobs;
int prompting = 0;
jmp_buf goto_prompt;
char initcd[256], currentcd[256];
void handler_sigchld(int sig_num, siginfo_t *info)
{
cell *job = trouver(&jobs, info->si_pid);
if (job && !job->state)
{
if (prompting)
{
printf("\n");
}
printf("[%d] %d done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
job_id--;
if (prompting)
{
siglongjmp(goto_prompt, sig_num);
}
}
}
void handler_sigint(int sig_num)
{
printf("\n");
siglongjmp(goto_prompt, sig_num);
}
void handler_sigtstp(int sig_num)
{
if (!prompting)
{
kill(pid_fils, SIGTSTP);
cell *job = ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 1);
printf("[%d] %d suspended: %s\n", job_id, pid_fils, job->cmd);
siglongjmp(goto_prompt, sig_num);
}
else
{
handler_sigint(sig_num);
}
}
void attendre(int pid)
{
int wait_code;
pid_t id = waitpid(pid, &wait_code, 0); // on attend la fin de l'exec du fils
if (id == -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, wait_code, strerror(wait_code));
}
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
getcwd(initcd, sizeof(initcd));
// 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);
action.sa_handler = handler_sigint;
sigaction(SIGINT, &action, NULL);
action.sa_handler = handler_sigtstp;
sigaction(SIGTSTP, &action, NULL);
// loop principal
while (1)
{
sigsetjmp(goto_prompt, 1);
prompting = 1;
getcwd(currentcd, sizeof(currentcd));
printf("%s >>> ", currentcd);
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;
}
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;
}
else if (!strcmp(cmd->seq[0][0], "jobs"))
{ // "jobs"
afficher(&jobs);
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{ // "bg"
kill(pid_fils, SIGCONT);
continue;
}
else if (!strcmp(cmd->seq[0][0], "fg"))
{ // "fg"
cell *job;
if (cmd->seq[0][1] == NULL)
{ // no id
fprintf(stderr, "ERROR: fg id error");
continue;
}
else
{ // id specified
job = &(jobs[atoi(cmd->seq[0][1]) - 1]);
}
kill(job->pid, SIGCONT);
printf("[%d] %d continued: %s\n", job->id, job->pid, job->cmd);
supprimer(&job, job->pid);
job_id--;
attendre(job->pid);
continue;
}
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
action.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &action, NULL); // on default SIGTSTP
sigaction(SIGCONT, &action, NULL); // on default SIGCONT
if (cmd->backgrounded)
{ // background
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
sigaction(SIGTSTP, &action, NULL); // on ignore SIGTSTP
}
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
attendre(pid_fils);
}
}
}
return EXIT_SUCCESS;
}

BIN
readcmd

Binary file not shown.

View file

@ -1,2 +1,2 @@
sleep 5
printf "\n\nbonjourr\n\n"
sleep 3
printf "\n\n\n\n bonjour \n\n\n\n"