0

「ls」または「ls -l」のようなものを取り、それを実行する単純なシェルを作成しようとしています。

これが私のコードです:

#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 *’

実行関数に引数を正しく渡す方法について、いくつかの指針を教えてください。

4

2 に答える 2

2

現在、単一の文字列を に渡していますがexecute()、これは文字列の配列を想定しています。適切に処理できる argsように、コンポーネントの引数に分割する必要があります。おそらく開始するのに適した場所です。execute()strtok(3)

于 2013-09-25T16:56:06.317 に答える
2
于 2013-09-25T16:56:20.083 に答える