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 103 104 105 106 107 108 109 110 111 112
|
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
typedef struct command
{
char chemin[256];
char instruction[128];
char argument[128];
}command;
void Execution(command cmde, int path)
{
pid_t fils;
printf("\nentrée fonction Execution :\n");
printf("cmde.instruction : %s\n",cmde.instruction);
printf("cmde.chemin : %s\n",cmde.chemin);
printf("cmde.argument : %s\n",cmde.argument);
fils=fork();
if (fils==0)
{
if (path>0)
{
printf("execl\n");
execl (cmde.instruction, cmde.chemin, cmde.argument, NULL);
}
else
{
printf("execlp\n");
execlp (cmde.instruction, cmde.argument, NULL);
}
exit(0);
}
}
void Interpretation (char* saisie)
{
int i=0,j,path=0,cmpt,args=0;
command cmde;
cmde.argument[0]='\0';
char* temp=strtok(saisie," ");
j=i;
strcpy(cmde.instruction,temp);
printf("cmde.instruction : %s\n",cmde.instruction);
for (cmpt=0;cmpt<=j;cmpt++)
{
i+=strlen(temp);
temp = strtok(0," ");
printf("saisie[%d] = %c\n",i, saisie[i]);
if (saisie[i]=='/')
{
path=1;
strcpy(cmde.chemin,temp);
printf("cmde.chemin : %s\n",cmde.chemin);
i+=strlen(temp);
}
else
{
if (args>0)
{
strcat(cmde.argument," ");
strcat(cmde.argument,temp);
}
else
{
strcpy(cmde.argument,temp);
}
printf("cmde.argument : %s\n",cmde.argument);
args++;
i+=strlen(temp);
}
i+=strlen(temp);
}
if (saisie[i-1]=='&')
{
printf("PID = %d\n",getpid());
}
Execution(cmde,path);
}
int main ()
{
char saisie[1500]={'\0'};
command cmde;
puts("Saisir une commande :");
fgets(saisie, 300, stdin);
strncpy(cmde.chemin, saisie, sizeof(cmde.chemin));
Interpretation(saisie);
return 0;
} |
Partager