Greg
Greg

Reputation: 65

Tokenizing input from getline

I'm trying to use getline() to take input from the keyboard, store it in a string, tokenize it, then print the tokens. When I run this, I get a Segmentation Fault error on the the last iteration (the iteration that handles the last token from the input).

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

////////////////////////
// Main Method        //
////////////////////////
void main() {
    system("clear");
    int ShInUse = 1; // Represents if shell is in use

    char curpath[1024];   // holds current path to print with prompt
    char *UserCommand = NULL;
    size_t combytes = 100;
    UserCommand = (char *) malloc(combytes);
    char *tok;

    // Main loop that shell uses //
    while (ShInUse == 1) {
        getcwd(curpath, sizeof(curpath)); // Store initial working dir
        printf("gash:%s>", curpath);   // print prompt

        getline(&UserCommand, &combytes, stdin);
        tok = strtok(UserCommand, " \n");   // Tokenize input
        if (tok == NULL ) {
            printf("Enter a command.\n");
        } else {
            // Exit command //
            if (strcmp(tok, "exit") == 0) {
                ShInUse = 0;
            } else {
                while (tok != NULL ) {
                    printf("You entered a command.\n");
                    printf("tok: %s\n", tok);
                    tok = strtok(NULL, " \n");
                }
            }
        }
    }
    free(UserCommand);
}

Any ideas as to what may be causing this? Debugging isn't an option for me at the moment.

Upvotes: 1

Views: 2602

Answers (2)

Steve Valliere
Steve Valliere

Reputation: 1207

Also not an answer, just another option for programming style:

Whenever I have a tokenizing loop like yours, I prefer to construct them like this:

for( tok = strtok( UserCommand, " \n" );
     tok != NULL;
     tok = strtok( NULL, " \n" ) )
{
   printf( "%s\n", tok );
}

This keeps both strtok() calls close together and requires writing the NULL test only once. Your way is fine, this is just another option. Good luck!

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129364

I tested your code with this:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *UserCommand = NULL;
    size_t combytes = 100;
    UserCommand = (char *) malloc(combytes);
    char *tok;
    while(getline(&UserCommand, &combytes, stdin) != EOF)
    {
    tok = strtok(UserCommand, " \n");    // Tokenize input
    if (tok != NULL) {
        while(tok != NULL) {
        printf("%s\n", tok);
        tok = strtok(NULL, " \n");
        }
    }
    }
    return 0;
}

and it works fine for all the testing I've done - including passing the source file in as input, writing quite long lines, etc, etc.

So my conclusion is that you probably have something ELSE that segfaults in your code.

Upvotes: 3

Related Questions