L'écriture d'une simple coque en C à l'aide de la fourche/execvp

Je dois développer une simple coque en C à l'aide d'appels système fork () et execvp(). Pour l'instant mon code dans une commande, répartit l'aide de strtok dans un tableau argv et puis j'appelle la fourchette pour créer un enfant et de l'exécution de la commande. Im travaillant sur ce dans ubuntu, où la plupart des commandes sont dans le /bin/répertoire, j'ai donc ajouter le nom du programme (par exemple /bin/ls) et de l'utiliser pour la première arg de execvp et puis je lui donner le tableau argv. Mon programme fonctionne si je tape la commande "ls", mais lorsque j'essaie d'autres commandes ou même "ls -l" je suis un "ls: option non valide." Im pas sûr de ce que je fais mal ici.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_LEN 1024

int main(){
    char line[BUFFER_LEN];  //get command line
    char* argv[100];        //user command
    char* path= "/bin/";    //set path at bin
    char progpath[20];      //full file path
    int argc;               //arg count

while(1){

    printf("My shell>> ");                    //print shell prompt

        if(!fgets(line, BUFFER_LEN, stdin)){  //get command and put it in line
        break;                                //if user hits CTRL+D break
    }
    if(strcmp(line, "exit\n")==0){            //check if command is exit
        break;
    }

    char *token;                  //split command into separate strings
    token = strtok(line," ");
    int i=0;
    while(token!=NULL){
        argv[i]=token;      
        token = strtok(NULL," ");
        i++;
    }
    argv[i]=NULL;                     //set last value to NULL for execvp

    argc=i;                           //get arg count
    for(i=0; i<argc; i++){
        printf("%s\n", argv[i]);      //print command/args
    }
    strcpy(progpath, path);           //copy /bin/to file path
    strcat(progpath, argv[0]);            //add program to path

    for(i=0; i<strlen(progpath); i++){    //delete newline
        if(progpath[i]=='\n'){      
            progpath[i]='
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 1024
int main(){
char line[BUFFER_LEN];  //get command line
char* argv[100];        //user command
char* path= "/bin/";    //set path at bin
char progpath[20];      //full file path
int argc;               //arg count
while(1){
printf("My shell>> ");                    //print shell prompt
if(!fgets(line, BUFFER_LEN, stdin)){  //get command and put it in line
break;                                //if user hits CTRL+D break
}
if(strcmp(line, "exit\n")==0){            //check if command is exit
break;
}
char *token;                  //split command into separate strings
token = strtok(line," ");
int i=0;
while(token!=NULL){
argv[i]=token;      
token = strtok(NULL," ");
i++;
}
argv[i]=NULL;                     //set last value to NULL for execvp
argc=i;                           //get arg count
for(i=0; i<argc; i++){
printf("%s\n", argv[i]);      //print command/args
}
strcpy(progpath, path);           //copy /bin/to file path
strcat(progpath, argv[0]);            //add program to path
for(i=0; i<strlen(progpath); i++){    //delete newline
if(progpath[i]=='\n'){      
progpath[i]='\0';
}
}
int pid= fork();              //fork child
if(pid==0){               //Child
execvp(progpath,argv);
fprintf(stderr, "Child process could not do execvp\n");
}else{                    //Parent
wait(NULL);
printf("Child exited\n");
}
}
} 
'
; } } int pid= fork(); //fork child if(pid==0){ //Child execvp(progpath,argv); fprintf(stderr, "Child process could not do execvp\n"); }else{ //Parent wait(NULL); printf("Child exited\n"); } } }
Vous n'avez pas besoin d'ajouter /bin le nom du fichier à moins que votre variable d'environnement PATH ne contient pas de /bin. Aussi, c'est dangereux, ce n'est probablement pas le problème, mais dangereux while(token!=NULL) faire while(token!=NULL && i < 100)

OriginalL'auteur C1116 | 2015-02-13