-
Parsing problem
Hi everyone, I'm currently working on a project in which I have to write a basic UNIX shell. My problem is with parsing the command line. I am using strtok to retrieve the first command (the program to be run) and putting it in a struct. I am having a problem with getting the argument values into an array of strings. I am trying to use strtok to get the args one by one, and then strcpy to put them into the array. I keep getting seg faults when I try to do this. Here is the relevant portions of the code:
struct command_t
{
char *name;
int argc;
char *argv[500];
};
int main()
{
char *in;
char *str;
struct command_t *command;
char *arguments[900];
while(1)
{
cout << "$$ ";
fgets(in, 100, stdin);
str = strtok(in, " ");
command->name = str;
str = strtok(NULL, " ");
strcpy(command->argv[0], str);
...
}
Using the debugger, I find the seg fault occurs right at the strcpy command. Any suggestions?
TIA
-
Cool, thanks a lot, that seems to work a lot better. Now, how could I allocate memory for all of the argv vectors(i.e. argv[2], argv[3], etc...), without using a loop if possible?