#include #include #include #include #include #include #include int main(void) { int pidfils,returncode; bool bCommand=true; char *pStrCommand; char binarypath[255]="/usr/bin/"; pStrCommand=calloc(255,sizeof(char)); fprintf(stdout, "Programme de test pour vérifier qu'il soit possible d'exécuter des commandes" " tant que \"exit\" ne soit pas encodé...\n"); while(bCommand==true) { fprintf(stderr,"[SHELL]>\t"); //fgets(pStrCommand,255,stdin); avec l'usage de execl il vaut mieux ne pas utiliser des fonctions de type "bufferisé"... int octetslus=read(STDIN_FILENO,pStrCommand,255); octetslus--; pStrCommand[octetslus]='\0'; pidfils=fork(); if(!pidfils) // FILS { fprintf(stderr,"[FILS] pStrCommand %s\n",pStrCommand); if(strstr(pStrCommand,"exit")!=NULL) exit(1); char *pParameters; pParameters=strchr(pStrCommand,' '); // chercher le premier espace... if(pParameters!=NULL) pStrCommand[pParameters-pStrCommand]='\0'; if(pParameters!=NULL) { ++pParameters; } strcat(binarypath,pStrCommand); fprintf(stderr,"[FILS] binarypath %s\n",binarypath); // A partir d'ici le Code Segment du processus sera remplacé par "ps" (ou n'importe quel autre programme exécutable) if(pParameters==NULL) execl(binarypath,pStrCommand,NULL); else execl(binarypath,pStrCommand,pParameters,NULL); } // PERE int code; returncode=wait(&code); if(returncode!=-1) { if(WEXITSTATUS(code)==1) bCommand=false; } free(pStrCommand); } } /* WIFEXITED(status) qui renvoie vrai si c'était exit(n) WEXITSTATUS(status) qui renvoie la valeur du "n" WIFSIGNALED(status) qui renvoie vrai si c'était kill(m) WTERMSIG(status) qui renvoie la valeur du "m" */