簡體   English   中英

為什么當我用硬編碼將execvp與ls一起工作時,但通過輸入將其讀入卻無法工作

[英]why does ls with execvp work when i hardcode it in but when I read it in through input it wont work

當我的代碼看起來像這樣時,我可以完美地運行ls -la。

    char * ls_args[3] = {"ls","-la",NULL};

                pid_t c_pid, pid;
                int status;

                c_pid = fork();

                if (c_pid == 0){
                     /* CHILD */

                     printf("Child: executing ls\n");

                     //execute ls                                                                                                                                                               
                    execvp( ls_args[0], ls_args);
                    //only get here if exec failed                                                                                                                                             
                    perror("execve failed");
                    }else if (c_pid > 0){
                     /* PARENT */

                        if( (pid = wait(&status)) < 0){
                            perror("wait");
                             _exit(1);
                         }

                         printf("Parent: finished\n");

                        }else{
                            perror("fork failed");
                            _exit(1);
                        }


            }

但是當我這樣做時,它不起作用,因為當我從行中讀取ls並將其傳遞給execvp時,這真是令人困惑,因為它與ls_args [0]一樣嗎? 但這種方式行不通。 任何幫助將是巨大的謝謝

        arguments[count] = NULL;
                pid_t c_pid, pid;
                int status;

                c_pid = fork();

                if (c_pid == 0){
                     /* CHILD */

                     printf("Child: executing ls\n");

                     //execute ls                                                                                                                                                               
                    execvp( arguments[0], arguments);
                    //only get here if exec failed                                                                                                                                             
                    perror("execve failed");
                    }else if (c_pid > 0){
                     /* PARENT */

                        if( (pid = wait(&status)) < 0){
                            perror("wait");
                             _exit(1);
                         }

                         printf("Parent: finished\n");

                        }else{
                            perror("fork failed");
                            _exit(1);
                        }


            }

arguments [0]中的內容為“ ls”。 當我檢查每個字符串的長度並比較它們的長度時,只要其中的字符串是同一回事,就可以了。

我完整的代碼

    int main(){

char *line;
int quit = 1;
char **arguments;
char *directory;
int count = 0;
char *comand = "ls";
int done  = 1;

printf("\n\n\n\n\n\n\nLinux Shell: \nCreated by: Zach Adams\n\n");
 //while this is one it will keep looping

while(1){ //will run until user enters quit
    done  = 1;
    count = 0;
    print_shell_name();
    line = (char *)malloc(sizeof(line));
    arguments = (char **)malloc(sizeof(char*));
    line = fgets(line,MAX,stdin);
    quit = strncmp(line,"quit",4);
    if(quit == 0)
        exit(0);

    int i = 0; //parsing line 
    char *p = strtok(line, " "); //will save the string up to the token entered
    free(line);
    while(p!= NULL){
        arguments[i++] = p; //putting the command or argument into an array of arguments
        p = strtok(NULL," "); //null is a pointer to the first argument and continues to scan where prev call ends until it gets to token again
        count++;
    }
    p = strtok(arguments[count-1], "\n"); //get the new line character off of the end 
    arguments[count-1] = p;
    if((strncmp(arguments[0],"cd",2)==0) || (strncmp(arguments[0],"clr",3)==0) //run internal commands
        || (strncmp(arguments[0],"dir",3) ==0) || (strncmp(arguments[0],"environ",7)==0) || (strncmp(arguments[0],"echo",4)==0)
            || (strncmp(arguments[0],"help",4)==0) || (strncmp(arguments[0],"pause",5)==0)){
                execute(arguments,count); //execute internal commands
            }else if(arguments[0] == NULL){
                printf("NULL arg");
            }else{
                //char * ls_args[3] = {"ls","-la",NULL};
                arguments[count] = NULL;
                pid_t c_pid, pid;
                int status;

                c_pid = fork();

                if (c_pid == 0){
                     /* CHILD */

                     printf("Child: executing ls\n");

                     //execute ls                                                                                                                                                               
                    execvp( arguments[0], arguments);
                    //only get here if exec failed                                                                                                                                             
                    perror("execve failed");
                    }else if (c_pid > 0){
                     /* PARENT */

                        if( (pid = wait(&status)) < 0){
                            perror("wait");
                             _exit(1);
                         }

                         printf("Parent: finished\n");

                        }else{
                            perror("fork failed");
                            _exit(1);
                        }


            }

            free(arguments);

            }

返回0;

}

這段代碼銷毀了您在以下位置使用strtok()line變量:

char *p = strtok(line, " "); //will save the string up to the token entered
free(line);

你打電話時

strtok(NULL," ")

稍后, strtok()仍在嘗試解析已釋放的line

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM