I want to use strtok() to divide a line into tokens, but the function seems to just make the first token and "destroying" the rest. I used printf() to check where did that happen and found out it was just after token= strtok(line,delimit)
.
Here's my main, read_line and parse_args functions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROMPT "$"
#define MAX_LINE 512
void main(){
char line[MAX_LINE];
while(read_line(line)){
execute_line(line);
}
}
char *read_line(char *line){
printf("%s ",PROMPT);
fflush(stdout);
line=fgets(line,MAX_LINE,stdin);
return line;
}
int parse_args(char **args, char *line){
printf("%s", line); //If it reads "Hello how are you?", here it prints properly
int n=0;
char* token;
char delimit[]=" \t\r\n\v\f";
token=strtok(line,delimit);
printf("%s", line); //however here it just prints "Hello"
while(token!=NULL){
printf("token%i: %s\n",n,token);
*args=token;
n++;
*args++;
token=strtok(NULL,delimit);
}
printf("token%i: %s\n",n,token);
*args=token;
return n;
}
int execute_line(char *line){
char **args;
parse_args(args,line);
check_internal(args);
return 0;
}