I'm trying to read user input on an infinite loop in a C program. The function that does this is as below.
void processUserInput(unsigned int io, char* cmd, char* buffer){
char* usrInput;
int rbytes;
if ((rbytes = read(io, buffer, 256)) < 0) {
perror("Read error: ");
exit(-1);
}
buffer[rbytes-1] = 0;
usrInput = (char*) calloc(256, sizeof(char));
strcpy(usrInput, buffer);
cmd = strtok(usrInput, " ");
}
In my main, this is what I do.
char buffer[256];
char* cmd = NULL;
processUserInput(STDIN_FILENO, cmd, buffer);
if(strcasecmp(cmd, "quit") == 0){
breakTrigger = 1;
}
memset(buffer, 0, 256);
The code gives a segmentation fault
the moment I enter an input on STDIN.
Any help appreciated.