Thread: Parsing problem

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    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

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    19
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem parsing 16-bit Hex to ASCII
    By black_spot1984 in forum C Programming
    Replies: 12
    Last Post: 12-05-2006, 11:13 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 05:02 PM