// Lab1 solution // Created by Brian Fraser. // Do not re-post or distribute beyond this class. #define _POSIX_C_SOURCE 200809L #include #include #include #include int main() { printf("Enter your command:\n> "); char *buff = NULL; size_t size = 0; // getline() allocates buffer, which we must free it. if (getline(&buff, &size, stdin) != -1L) { // Tokenize the string: char *input_str = buff; char *delim = " \t\n\r"; char *token = NULL; char *saveptr = NULL; // input_str must contain the real string on the first call, but be NULL // later. saveptr must be null the first time, and is set later (stores // status so call is reentrant) while ((token = strtok_r(input_str, delim, &saveptr))) { printf("Token: '%s'\n", token); input_str = NULL; } } else { printf("Getline failure.\n"); } free(buff); }