0
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

void exec(char **args){
    pid_t  pid;
    int status;
    if ((pid = fork()) < 0) {    
        printf("*** ERROR: forking child process failed\n");
        exit(1);
    }
    else if (pid == 0) {      
        if(execvp(args[0],args)<0)//{
            //printf("argv[0]=%s argv[1]=%s",args[0],args[1]);
            printf("**error in exec\n");
    }
    else {                                  
        while (wait(&status) != pid);
    }
}

void exec2(char **args, char *file){
    printf("file =%s\n",file);
    int fd;
    pid_t pid;
    int status;
    if ((pid = fork()) < 0) {    
        printf("*** ERROR: forking child process failed\n");
        exit(1);
    }

    else if (pid == 0) {   
        fd = open(file, O_RDWR | O_CREAT, (mode_t)0600);
        close(1);
        dup2(fd, 1);
        if(execvp(args[0],args)<0){
            printf("**error in exec");
        }

        else {  
            printf("\nhere\n");
            close(fd);                                
            while (wait(&status) != pid){
                fflush(stdout) ;
            }
    }
}
    close (fd);
}


void main(){
    char *command;
    char inp[512];
    char *filepath;
    size_t size=0;
    char *substr;
    char *args[512];
    command = (char *) malloc(sizeof(char *) * 512);
    int flag=0;
    int redirect=0;
    int i=0;
    while (1){
        printf("$ ");
        command = fgets(inp, 512/*sizeof(char *)*/, stdin);

        command[strlen(command)-1]='\0';

        if (strchr(command,'>')){
            redirect=1;
            strtok_r(command,">",&filepath);
        }

        size_t siz=4;
        //printf("command=%s\n",command);
        int i=0;
        while(1){
            //printf("i=%d\n",i);
            char *tok = strtok_r(command," ",&substr);
            if (tok==NULL){
                break;
            }
            args[i++] = tok;
/*              printf("tok=%s\n",tok);
            printf("len tok = %d\n",(int)strlen(tok));
            printf("command=%s\n",command);
            printf("substr=%s\n",substr);     
*/           command = substr;
    }

    //printf("args[0]=%s",args[0]);
    if (!strncasecmp(args[0],"exit",siz) || !strncasecmp(args[0],"quit",siz))
    {
        printf("\nBye\n");
        exit(0);
    }

    else if(strcmp(args[0],"cd")==0){
        chdir(args[1]);
        //printf("chdir")   ;
        //system("pwd");
} 

else if (redirect==1){
    exec2(args,filepath);
}

else exec(args);
}
}

さて、これは私のシェルのコードです。私がそれを実行するとき、私はそれを置きls、それは正しい出力を与えます。それから私は置いls -lて、それから再びlsします、そしてそれは与えます:

ls: cannot access : No such file or directory

また、を使用するcdと、ls出力が表示されず、次のように表示されpwdます。

"ignoring unused arguments"

またcat、動作しません。でもmkdir、動作psls -lます。

4

1 に答える 1

2

コードにいくつかの問題があります。最初に512を超える方法を割り当てます。これは、おそらく4commandを使用するためです。sizeof(char*)

command = (char *) malloc(sizeof(char*) * 512);

代わりに、を使用sizeof(char)てください。mallocの結果をキャストするべきではありません。その背後にはいくつかのロジックがありますが、それはおそらく好みの問題です。私は個人的にはしません。

command = malloc(sizeof(char) * 512);

次に、strtok_r正しく使用していません。最初に呼び出したときに文字列を渡し、その後は渡すNULLので、次のようになります。

char *tok = strtok_r(command, " ", &substr);
while(tok){
    args[i++] = tok;
    tok = strtok_r(NULL, " ", &substr);
}

最後に、これが主な問題であり、最後の引数は次のようにする必要がありますNULL

args[i] = 0;
于 2012-11-12T16:53:32.580 に答える