First of all, I create two windows with ncurses : one for transmission and one for reception. Basically, one to write the command and the other to print the command. To write the input in the reception window i did a little function to concatenate the string already in the reception window and the one written by the user (i think the problem is in there).
So, when I run the code, the program fails and displays Segmentation Fault code dumped at the line wrefresh(winReception);
But the weird thing is, if the input is 7 characters or less it works, if it's 8 or more it breaks.
I'm using Code::Blocks
Screenshots here : https://i.stack.imgur.com/JTS12.jpg
Here is some code :
//Global variables
WINDOW * winReception;
WINDOW * winTransmission;
char * command;
char mesg[] = "Enter a command";
//variable to stock the input in reception window
char *textinwindow = "";
//main
int main(int argc, char* argv[])
{
initscr();
/* WINDOW RECEPTION */
winReception = newwin(15, 0, 0, 0);
wrefresh(winReception);
/* WINDOW TRANSMISSION*/
winTransmission= newwin(8, 0, 15, 0);
wrefresh(winTransmission);
mvwprintw(winTransmission, 1, 2, mesg);
wgetstr(winTransmission, &command);
verifInput(&command);
free(textinwindow);
exit(0);
}
//concat function (where I think the bug is)
char* concat(char *s1, char *s2)
{
char *result = (char *) malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, "\n ");
strcat(result, s2);
return result;
}
//verifInput (where the program fails)
void verifInput (char* cmd)
{
/* WINDOW RECEPTION */
textinwindow = concat(textinwindow, cmd);
mvwprintw(winReception, 1, 2, textinwindow);
wrefresh(winReception);
/* WINDOW TRANSMISSION*/
touchwin(winTransmission);
wclear(winTransmission);
wrefresh(winTransmission); //Program fails here
mvwprintw(winTransmission, 1, 2, mesg);
wgetstr(winTransmission, &command);
verifInput(&command);
}