簡體   English   中英

為什么我的 C shell 沒有輸出任何東西?

[英]Why isn't my C shell outputting anything?

處理 class 的項目。 我們應該寫一個 C shell。 我找到了一堆很好的例子,但對於我的生活,我無法讓我的版本生成任何 output。

它不斷打印提示,但沒有別的。

我已經跟隨一些例子幾乎逐字地試圖解決這個問題,但仍然沒有。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <stdbool.h>

#include <sys/wait.h>



/******************************************

  @brief Fork a child to execute the command using execvp. The parent should wait for the child to terminate

  @param args Null terminated list of arguments (including program).

  @return returns 1, to continue execution and 0 to terminate the MyShell prompt.

******************************************/

int execute(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, args) < 0) {     
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
        }
        else 
        {                                  
          while (wait(&status) != pid)       
               ;
        }
        
        return 0;

}





/******************************************

  @brief gets the input from the prompt and splits it into tokens. Prepares the arguments for execvp

  @return returns char** args to be used by execvp

******************************************/

char** parse(void)

{

    //Get the string and store it. Remove newline.  

    char strIn[255];

    printf("MyShell>");

    fgets(strIn, sizeof(strIn), stdin); 

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

    

    //Array, token, counters. 

    char *args[20]; 

    char *tok; 

    int i = 0;

    int count = 0;

    

    //Allocate array. 

    for(i = 0; i < 20; i++)

    {

        args[i] = (char*)malloc(20 * sizeof(char));

    }

    

    //Fill in args[0]

    tok = strtok(strIn, " ");

    strcpy(args[count++], tok);

    

    //Fill in array with tokens. 

    while (tok != NULL)

    {

        tok = strtok(NULL, " ");

        

        if (tok == NULL)

            break;

            

        strcpy(args[count++], tok);

    }

    

    //Null terminate.

    args[count] = NULL;

        

    return args; 

}





/******************************************

   @brief Main function should run infinitely until terminated manually using CTRL+C or typing in the exit command

   It should call the parse() and execute() functions

   @param argc Argument count.

   @param argv Argument vector.

   @return status code

******************************************/

int main(int argc, char **argv)

{

    bool run = true; 

    

    while(run)

    {

        char** argArr = parse(); 

        execute(argArr);


        }

        

    return 0;

}

output,不管我做什么,都是:

MyShell>
MyShell>
MyShell>

有人能告訴我哪里出錯了嗎?

parse()返回一個指向本地數組args的指針。 由於args的生命周期在parse()返回時結束,因此任何使用parse()的返回值的嘗試都是未定義的行為。 您應該使用malloc()分配該數組(並稍后釋放它。)。

在我的測試中發生的是編譯器注意到parse()的返回值不能合法使用(它會發出警告!!你應該閱讀並注意!! ),所以它只返回NULL . 當孩子將此指針取消引用為*args以獲取execvp的第一個參數時,它會出現段錯誤並在不調用execvp()的情況下死亡。 如果您檢查wait()返回的status ,您可以檢測到這一點,但您沒有。 所以看起來孩子什么也沒做。

哦,額外的錯誤:當stdin上出現文件結束時(例如,如果您按 Ctrl-D), fgets()返回的字符串將為空並且strIn[strlen(strIn)-1]='\0'; 將存儲超出范圍的 null 字節。 您需要測試fgets()的返回值。

暫無
暫無

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

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