我正在編寫一個程序,它需要一個命令行然後解析它,以便在輸入中打印每個argv的字符串數組。解析execve的命令行()
該代碼給我一個分段錯誤(核心轉儲)!
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
char **parse(int position ,char *argv[] ) ;
int main(int argc ,char *argv[])
{
int i=1;
int f=argc;
argc--;
while(i<f)
{
char commands[10];
char **argument=parse(argc,argv);
//parse(i ,argv ,commands ,argument) ;
printf("the argument[ %i ] is :%s \n",i,argument[i]);
argc-- ;
i++;
}
}
char **parse(int position ,char *argv[])
{
// char *commands;
char** arguments;
char *result ;
char buffer [30] ;
int count =0;
arguments = calloc(1, sizeof (char *));
strcpy(buffer,argv[position-1]); //copy the current argv to the buffer
result =strtok(buffer," ");
// strcpy(commands,result);
//result =strtok(buffer," ");
while(result !=NULL)
{
arguments[count] =result ;
++count;
arguments = realloc(arguments, sizeof (char *) * (count + 1));
result=strtok(NULL," ");
}
arguments[count] = NULL; //in order to call the execvp
return arguments;
}
謝謝你的幫忙。
Y你永遠不允許'void' main()。 – 2013-05-12 05:48:20
單詞。已更改.... – 2013-05-12 08:00:40