0

For my project I need to create a shell and I need the input to be in the format char**, so it can be used better with the system calls. Currently I have the following to parse the input.

char **ParseInput(char *args)
{
    char **tokenized_args = new char *[MAX_BUFFER_WORDS];

    char *token = strtok(args, " ");

    int i = 0;
    while (*token != '\n')
    {
        tokenized_args[i] = token;
        token = strtok(args, " ");
        i++;
    }

    return tokenized_args;
}

However I keep getting a segmentation fault at tokenized_args[i] = token; and I can't really figure out why.

Any help is appreciated.

  • 1
    strtok is only passed the string once - the first time. Each successive time you should pass NULL for the string. – Jerry Jeremiah Mar 04 '21 at 03:24
  • Are you trying to write C or C++ code? `new` implies C++ but you have tagged as C. They are different languages so just want to confirm what your actual intention is. – kaylum Mar 04 '21 at 03:25
  • strtok returns NULL when there is nothing left to parse, so your while condition should be `while (token && *token != '\n')` – Jerry Jeremiah Mar 04 '21 at 03:26
  • The overall project is in c++, but I am writing this particular section in C style to help when I need to make the system calls – Colin Harker Mar 04 '21 at 03:26
  • also I should have noted, when I run it in gdb, it says I get the error when `tokenized_args[i] = token;` and `token = "random_input' – Colin Harker Mar 04 '21 at 03:28
  • I think the error with `tokenized_args[i] = token;` is the while condition. – Jerry Jeremiah Mar 04 '21 at 03:30
  • I changed the while condition to `while (token && *token != '\n')` and I am still getting the same error – Colin Harker Mar 04 '21 at 03:31
  • If you are strtok with ' ' as the delimiter then the odds of *token being '\n' is really small because it would have to come by itself after a space... – Jerry Jeremiah Mar 04 '21 at 03:34
  • Did you change the second `strtok(args, " ");` to be `strtok(NULL, " ");`? Also, you should protect against `i` getting larger than the array bounds. Which is likely what is happening due to your incorrect `strtok` call and possibly incorrect `while` condition. – kaylum Mar 04 '21 at 03:36
  • I think i is going out of bounds because strtok is returning the first word over and over. Changing the two things I suggested makes it work for me: https://onlinegdb.com/BJdN8R6Gd – Jerry Jeremiah Mar 04 '21 at 03:38
  • Yeah it seams that my problem was not setting the second strtok to null. And I did fix my out of bounds error as well. Would you guys be able to expand on why strtok has to be null after the first call? – Colin Harker Mar 04 '21 at 03:41
  • [why do we use NULL in strtok()?](https://stackoverflow.com/questions/23456374/why-do-we-use-null-in-strtok). Basically `strtok` internally stores a pointer to where it is up to in the string the first time it is called with non-NULL. Each NULL call tells it to continue from the last call. That's why `strtok` can only work on one string at a time. `strtok_r` removes that restriction. – kaylum Mar 04 '21 at 03:44
  • @ColinHarker : Why is this tagged _shell_? – user1934428 Mar 04 '21 at 10:20

0 Answers0