我正在嘗試創建一個簡單的shell,它需要類似「ls」或「ls -l」的東西併爲我執行它。期望'char **',但參數的類型是'char *'--argv
這裏是我的代碼:
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
void execute(char **argv)
{
int status;
int pid = fork();
if ((pid = fork()) <0)
{
perror("Can't fork a child process\n");
exit(EXIT_FAILURE);
}
if (pid==0)
{
execvp(argv[0],argv);
perror("error");
}
else
{
while(wait(&status)!=pid)
;
}
}
int main (int argc, char **argv)
{
char args[256];
while (1)
{
printf("shell>");
fgets(args,256,stdin);
if (strcmp(argv[0], "exit")==0)
exit(EXIT_FAILURE);
execute(args);
}
}
我收到以下錯誤:
basic_shell.c: In function ‘main’:
basic_shell.c:42: warning: passing argument 1 of ‘execute’ from incompatible pointer type
basic_shell.c:8: note: expected ‘char **’ but argument is of type ‘char *’
能否請您給我介紹瞭如何正確傳遞參數給我的執行功能一些指針?
每當我看到有人問一些指點我記得xkcd漫畫... – streppel