From a1c8231640c11a3320f29712c3383075faa8bf84 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 13 Feb 2023 11:08:57 +0300 Subject: [PATCH] Added segmentation fault :) --- Makefile | 2 ++ main.c | 23 ++++++++++++++--------- test.c | 4 ++-- test.txt | 1 + 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 Makefile create mode 100644 test.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f3cfb68 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + gcc main.c -lncurses -g diff --git a/main.c b/main.c index e37cb0f..3e82081 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,7 @@ void clearInput() { void printContent(char (*content)[cols+1]) { for (int i = rows - 2; i > 1; i --) { - mvprintw(rows - i, 0, "%s", content[rows - 2 - i]); + mvprintw(i, 0, "%s", content[rows - 2 - i]); } } @@ -22,19 +22,19 @@ void insertInStartContent(char (*content)[cols+1], char* toInsert) { for (int i = rows; i > 0; i --) { strcpy(content[i], content[i - 1]); } - strcpy(content[0], toInsert); + strcpy(content[rows], toInsert); } int main (int argc, char* argv[]) { initscr(); - // cbreak(); + cbreak(); keypad(stdscr, TRUE); noecho(); char ch = ' '; int len = 0; - - char (*content)[cols+1] = malloc(sizeof(char[rows][cols+1])); + char* cmd_string = malloc(cols); + char (*content)[cols] = malloc(sizeof(char[rows][cols+1])); for (int i = 0; i < rows; i ++) { strcpy(content[i], " "); @@ -44,16 +44,21 @@ int main (int argc, char* argv[]) { while (ch = getch()){ switch (ch) { - case 10: - char* toInsert = malloc(cols+1); - mvscanw(rows - 1, 0, "%s", toInsert); - insertInStartContent(content, toInsert); + case 10: { + // char* toInsert = malloc(cols+1); + FILE* f = fopen("test.txt","w"); + fprintf(f, "test: %s", cmd_string); + fclose(f); + insertInStartContent(content, cmd_string); len = 0; clearInput(rows, cols); + cmd_string = ""; refresh(); break; + } default: mvprintw(rows - 1 , len, "%c", ch); + cmd_string[len] = ch; len ++; break; } diff --git a/test.c b/test.c index 5e4a117..09c335e 100644 --- a/test.c +++ b/test.c @@ -13,10 +13,10 @@ void insert (char (*content)[cols], char* toInsert) { } int main () { - cols = 20; + cols = 50; rows = 3; - char (*content)[cols] = malloc(sizeof(char[rows][cols+1]) * 10); + char (*content)[cols] = malloc(sizeof(char[rows][cols+1])); strcpy(content[0], "Test string 1"); strcpy(content[1], "Test string 2"); diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..08a7839 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +test: test \ No newline at end of file