一度に文字列 (a) を解析する関数を作成しようとしていますchar *
が、何らかの理由でセグメンテーション エラーが発生します。ユーザー入力を読み込んで、プログラム名と引数を解析しようとしていますが、それはやや不謹慎です。
コンソール:
>./mish
>mish>ls
>command start:ls
>*tempCommand:l
>bottom
>Segmentation fault
コード:
ParserData parseCommand(ParserData parserData, char* command)
{
char* tempCommand;
char* currToken = '\0';
short firstSpace = 0;
printf("command start:%s \n", command);
strcpy(tempCommand, command);
while(*tempCommand)
{
printf("*tempCommand:%c \n", *tempCommand);
if((char)*tempCommand == ' ' && firstSpace == 0)
{
printf("currToken: %c \n", (char)*currToken);
strcpy(parserData.myChildProgramName,currToken);
printf("after:");
currToken = '\0';
firstSpace = 1;
}
else if((char)*tempCommand != ' ' && *tempCommand != '-')
{
//strcat(currToken, tempCommand);
}
printf("bottom\n");
printf("currToken: %c \n", *currToken);
tempCommand++;
}
printf("out\n");
parserData.myChildProgramArguments = currToken;
return parserData;
}