From 58de733df4a4e486cc7eec2d535d0d974825df17 Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Sun, 24 Sep 2023 04:37:17 +0500 Subject: [PATCH] Fix the bug I was crying about in the last commit It was a pretty simple fix. I used gdb to find it, was fun. --- Makefile | 6 +++++- README.md | 2 +- src/buffer.c | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 32478bc..095a853 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ WARNING_FLAGS := -Wall -Wextra INCLUDE_FLAGS := -I include CC := clang -CFLAGS := $(WARNING_FLAGS) $(INCLUDE_FLAGS) -DKILO_COMMIT_HASH=$(HASH) -MMD -MP -std=c99 -ggdb +CFLAGS := $(WARNING_FLAGS) $(INCLUDE_FLAGS) -DKILO_COMMIT_HASH=$(HASH) -MMD -MP -std=c99 -g kilo: $(OBJS) $(CC) $(CFLAGS) $^ -o $@ @@ -16,6 +16,10 @@ $(OBJS): build/%.o: src/%.c @mkdir -p build $(CC) $(CFLAGS) -c $< -o $@ +.PHONY: install clean +install: kilo + cp kilo ~/bin + clean: rm -rf build rm -f ./kilo diff --git a/README.md b/README.md index b1b3cf2..64786d5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # kilo My implementation of the [kilo text editor][1], which I wrote by following -[this tutorial][2]. The current state is horribly bugged, regrettably so. +[this tutorial][2]. ## How to build diff --git a/src/buffer.c b/src/buffer.c index a016323..1943295 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -186,14 +186,16 @@ static char *buffer_get_string(struct buffer *buffer, size_t *n_chars) { for (int i = 0; i < buffer->n_rows; i++) *n_chars += buffer->rows[i]->n_chars + 1; + *n_chars -= 1; char *write_buffer = malloc(*n_chars); - for (int i = 0, j = 0; i < buffer->n_rows; j++, i++) { + for (int i = 0, j = 0; i < buffer->n_rows; i++) { struct erow *row = buffer->rows[i]; memcpy(write_buffer + j, row->chars, row->n_chars); j += row->n_chars; - write_buffer[j++] = '\n'; + if (i < buffer->n_rows - 1) + write_buffer[j++] = '\n'; } return write_buffer;