預設“ls”和execvp“ls”呼叫的區別

預設“ls”和execvp“ls”呼叫的區別

我使用 ac 檔案內部的 execvp 系統呼叫呼叫了“ls”命令。但似乎還是有細微的差別。

在此輸入影像描述

預設 ls 呼叫中的 exe 檔案突出顯示為綠色,但在我自己的 execvp ls 呼叫中卻沒有。我錯過了什麼嗎?

這是呼叫 ls 的程式碼片段。

else if (rc == 0) 
{ 
    // child (new process)
    printf("hello, I am child (pid:%d)\n", (int) getpid());
    char *myargs[2];
    myargs[0] = strdup("ls");
    // program: "wc" (word count)
    myargs[1] = NULL;//strdup("p3.c"); // argument: file to count
    //myargs[2] = NULL;
    // marks end of array
    execvp(myargs[0], myargs); // runs word count
    printf("this shouldn’t print out");
}

答案1

ls在你的 shell 中有一個別名ls --color=auto

$ alias ls
alias ls='ls --color=auto'

ls如果您想在輸出中著色,請使用該--color選項。

相關內容