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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
int status;
int main(int argc, char *argv[])
{
char buffer[500];
memset(buffer,0,500);
if( argc == 2){
printf("argc =2 argv[1] = nom du fichier a traiter %s\n", argv[1]);
}
printf("\nDans MON SHELL écrire \"exit\" pour quitter\n");
while(1){
printf("MONSHELL > : ");
fgets(buffer,25,stdin);
int pid = fork();
if(pid == 0){
if (strstr(buffer,"exit") != NULL)
{
printf("PID : %d PPID : %d\n", getpid(), getppid());
exit(0);
}
if(strstr(buffer,"dir") != NULL)
{
execl("/bin/ls", "ls", "-l", NULL);
}
if(strstr(buffer,"ctr") != NULL)
{
execl("/usr/bin/clear", "clear", NULL);
}
if(strstr(buffer,"environ") != NULL)
{
execl("/usr/bin/env", "env", NULL);
}
if(strstr(buffer,"echo") !=NULL)
{
//char *argsexpr = "\\(.*\\).$";
//execl("/usr/bin/expr", "&buffer[5]", ":", "argsexpr", NULL);
//expr "bonjour" : "\(.*\).$"
//execl("/usr/bin/cut", "cut", "-f3-6", "&buffer[5]", NULL);
printf("%s\n", &buffer[5]);
}
else
{
printf("Commande inconnue ...\n");
}
}//fin if==0
int ret = waitpid(pid, &status, 0);
printf("ret = %d\n", ret);
if(ret == -1){
perror("echec waitpid du prc enfant 1");
}else{
printf("le execl() est termine prc = %d\n", getpid());
if (strstr(buffer,"exit") != NULL){
exit(0);
}
}
if(pid == -1)
{
perror("erreur fork -1");
exit(EXIT_FAILURE);
}
} //fin while
return EXIT_SUCCESS;
}//fin main |