formatting

This commit is contained in:
Laureηt 2021-04-20 18:50:27 +02:00
parent ab6da28992
commit 3295e0df03
5 changed files with 111 additions and 77 deletions

BIN
jobs

Binary file not shown.

3
jobs.c
View file

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "jobs.h" #include "jobs.h"
void ajouter(list* l_ptr, int pid, int id) { void ajouter(list* l_ptr, int pid, int id) {
@ -42,8 +43,6 @@ void supprimer(list* l_ptr, int pid) {
cursor->next = cursor_next; cursor->next = cursor_next;
} }
} }
void afficher(list* l_ptr) { void afficher(list* l_ptr) {

8
jobs.h
View file

@ -3,8 +3,10 @@
typedef struct cell cell; typedef struct cell cell;
struct cell { struct cell {
int pid;
int id; int id;
int pid;
int state;
char* cmd;
cell* next; cell* next;
}; };
@ -18,3 +20,7 @@ void liberer(list* list);
int contiens(list* l_ptr, int pid); int contiens(list* l_ptr, int pid);
#endif #endif
// state:
// 0: running
// 1: suspended

BIN
minishell

Binary file not shown.

View file

@ -15,16 +15,22 @@ int prompting = 0;
int job_id = 1; int job_id = 1;
list jobs; list jobs;
void handler_print(int signal_num, siginfo_t* info) { void handler_print(int signal_num, siginfo_t *info)
if (contiens(&jobs, info->si_pid)) { {
if (prompting) { printf("\n"); } if (contiens(&jobs, info->si_pid))
{
if (prompting)
{
printf("\n");
}
printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num)); printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num));
supprimer(&jobs, info->si_pid); supprimer(&jobs, info->si_pid);
job_id--; job_id--;
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
extern int errno; extern int errno;
@ -36,64 +42,87 @@ int main(int argc, char *argv[]) {
sigemptyset(&action.sa_mask); sigemptyset(&action.sa_mask);
sigaction(SIGCHLD, &action, NULL); sigaction(SIGCHLD, &action, NULL);
char initcd[256], currentcd[256]; char initcd[256], currentcd[256];
getcwd(initcd, 256); getcwd(initcd, 256);
while (1) { while (1)
{
getcwd(currentcd, 256); getcwd(currentcd, 256);
printf("%s >>> ", currentcd); printf("%s >>> ", currentcd);
prompting = 1; prompting = 1;
struct cmdline* cmd = readcmd(); struct cmdline *cmd = readcmd();
prompting = 0; prompting = 0;
if (cmd == NULL || cmd->seq[0] == NULL) { // ligne vide, on skip if (cmd == NULL || cmd->seq[0] == NULL)
{ // ligne vide, on skip
continue; continue;
} else if (!strcmp(cmd->seq[0][0], "exit")) { // on quitte le shell }
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // on quitte le shell
break; break;
} else if (!strcmp(cmd->seq[0][0], "cd")) { // cd }
if (cmd->seq[0][1] == NULL) { // vide else if (!strcmp(cmd->seq[0][0], "cd"))
{ // cd
if (cmd->seq[0][1] == NULL)
{ // vide
chdir(initcd); chdir(initcd);
} else { // avec un path }
else
{ // avec un path
chdir(cmd->seq[0][1]); chdir(cmd->seq[0][1]);
} }
continue; continue;
} else if (!strcmp(cmd->seq[0][0], "jobs")) { }
else if (!strcmp(cmd->seq[0][0], "jobs"))
{
afficher(&jobs); afficher(&jobs);
continue; continue;
} }
pid_t pidFils = fork(); pid_t pidFils = fork();
if (pidFils == -1) { if (pidFils == -1)
{
fprintf(stderr, "ERROR: forking failed, %s\n", strerror(errno)); fprintf(stderr, "ERROR: forking failed, %s\n", strerror(errno));
exit(errno); exit(errno);
} }
if (pidFils == 0) { // fils if (pidFils == 0)
execvp(cmd->seq[0][0], cmd->seq[0]); { // fils
int test = execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur exit(errno); // si execlp échoue on exit avec une erreur
}
} else { // père else // père
if (cmd->backgrounded) { {
if (cmd->backgrounded)
{
ajouter(&jobs, pidFils, job_id++); ajouter(&jobs, pidFils, job_id++);
} else { }
else
{
jmp_buf env; jmp_buf env;
setjmp(env); setjmp(env);
int codeTerm; int codeTerm;
pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils
if (idFils == -1){ if (idFils == -1)
{ // si le wait fail
// todo : peut mieux faire ? if (errno == 4)
if (errno == 4) { { // todo : peut mieux faire ?
longjmp(env, 1); longjmp(env, 1);
} }
else
{
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno)); fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno));
exit(errno); exit(errno);
} }
}
if (codeTerm)
{
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pidFils, codeTerm, strerror(codeTerm));
}
} }
} }
} }