-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathENSEASH_Q4.c
84 lines (61 loc) · 1.66 KB
/
ENSEASH_Q4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "fonction.h"
ssize_t nblus;
size_t nbytes=sizeof(buffer);
const char* instruction0="fortune";
const char* exitInstruction="exit";
char exitINFO[50];
char signalINFO[50];
int pid; //ientifiant du processus
int status; //satut du processus
int main(){
char * hello = "$ ./enseash\nBienvenue dans le Shell ENSEA\nPour quitter taper 'exit'.\nenseash % ";
printSTR(hello);
while(1){
readInstruction();
testInstruction(buffer);
//GESTION DE L'ERREUR//
// Si la chaine == "EXIT" ou si la chaine n'a lu aucun caractère i.e <ctrl+d> pressé on quitte.
if(strcmp(buffer,exitInstruction)==0 || nblus==0){
printSTR("\n");
exit(EXIT_SUCCESS);
break;
}
pid=fork();
if(pid>0){
wait(&status);
if(WIFEXITED(status)){
sprintf(exitINFO,"enseash [exit : %d] :", WEXITSTATUS(status));
printSTR(exitINFO);
}
if(WIFSIGNALED(status)){
sprintf(signalINFO, "enseash [signal exit : %d] :", WTERMSIG(status));
printSTR(signalINFO);
}
}else{
execlp(buffer,buffer,(char*)NULL);
exit(-1);
}
}
return(0);
}
// void printSTR(char*)
// @return affiche le char*
// @param une chaine de caractere
void printSTR(char* mot){
write(STDOUT_FILENO,mot,strlen(mot));
}
// void readInstruction(void)
// lit une chaine de caractère rentréé par l'utilisateur
void readInstruction(void){
nblus=read(STDIN_FILENO,buffer,nbytes);
buffer[nblus-1]=0;
}
// void testInstruction(char*)
// effectue les actions de la chaine de caractère
// @param la chaine rentréé par l'utilisateur
//
void testInstruction(char* buffer){
if(strcmp(buffer,instruction0)==0){
printSTR("Today is what happened to yesterday \n");
}
}