Bonjour!

J'essaye actuellement de créer mon propre shell, que j'ai décomposé en une fonction de parsing et une fonction de commande.
J'arrive à afficher le prompt mais je n'arrive pas encore à effectuer une commande simple. je rencontre l'erreur suivant:

"Erreur de segmentation (core dumped)"
quand je lui applique la commande ls.

Et lorsque je fais un retour à la ligne, il ne m'affiche rien et sort de mon shell.
Comment faire pour qu'il n'en sorte pas et pour traiter cette erreur de segmentation?

Voici mon code:

la méthode parsing

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
int parsing(){
 
    int i=0;
    int cmot=0;
    fprintf(stderr,"debut parsing\n");
 
    while(1){
        c = getchar();
 
        fprintf(stderr,"cartographie-lu %d %c\n",c,c);
        if      (c == '\n') {symboleP = 0;return 0;}
        else if (c == ';')  {symboleP = 1;return 1;}
        else if (c == '&')  {symboleP = 2;return 2;}
        else if (c == '<')  {symboleP = 3;return 3;}
        else if (c == '>')  {symboleP = 4;return 4;}
        else if (c == '|')  {symboleP = 5;return 5;}
        //else if (c == ' ')  {symboleP = 6;return;}
        else if (c == EOF)  {symboleP = 7;return 7;}
        else if (c != ' ') {
 
            symboleP = 10;
 
            while(c != '\n' && !strchr(delimiteurs,c)){
                i=0;
 
                while(c != 32 ){
                    if((c != '\n') && !strchr(delimiteurs,c)){
                    //fprintf(stderr,"boucle\n");
                    mot[i]=c;i++;
                    c=getchar();/*fprintf(stderr,"val carac %d\n",c);*/
                    }
                    //fprintf(stderr,"fin de mot\n");
                    else {/*fprintf(stderr,"break\n");*/break;}
 
                }
                break;
 
 
            }
            while(c == ' '){c=getchar();}
            ungetc(c,stdin);
            mot[i]='\0';
            resP[cmot++]=strdup(mot);
            fprintf(stderr,"elt comm lue %s %s\n",resP[0],resP[1]);
            if(c == '\n' || strchr(delimiteurs,c)){resP[cmot]=0;if(c!='\n')ungetc(c,stdin);return 10;}
        }
 
    }
        fprintf(stderr,"elt comm lue %s %s\n",resP[0],resP[1]);
 
 
}
la méthode commande:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
int commande(int fin,int fout,char * com, char * param, int *bg){
        int s;
       parsing();
        fprintf(stderr,"résultat parsing %d\n",s);
 
    switch(symboleP){
        case 0: // NL
 
 
            pid=fork();
                if(pid==0){
 
 
                    resCommande = 2;
                    execvp(resP[0],resp);
                }else{
                    if(bg==0){                //pas de bg on attend le fils
                        waitpid(pid, NULL, 0);
                }            
                    execute=1;
 
		}
 
		break;
 
 
    /*    case 1: // ;
        case 2: // &
        case 3: // <
        case 4: // >
        case 5: // |
        case 7: // EOF
        case 10:       // mot*/
 
           	default:
                printf(" ");
 
        }
 
    return 0 ;
}
et le main:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
int main(int argc, char* argv[]) {
 
        int fmess = open("imp", O_WRONLY|O_TRUNC|O_CREAT, 0640);
        close(2);
        dup(fmess);
        close(fmess);
        char *com[20];
        int status;
        int bg;
        //int eof= 0;
        printf("DAUPHINE> ");
        fflush(stdout);
 
        while(1){
        if(resCommande ==2){
            resCommande = 0;
 
            printf("DAUPHINE> ");
            fflush(stdout);
            }
        else{
            commande(0,1,&com,&param,&bg);// c'est ou param?
            }
        return 0;
}
 
}


Merci pour votre aide!