diff options
author | Oskar <[email protected]> | 2024-04-20 13:31:23 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-04-20 13:31:23 +0200 |
commit | 7412fe41acf3ddecc732a5080443b0d3d954c97c (patch) | |
tree | fd126b3b38b4eff9ed79a7b5c61f2b26a19d2046 | |
parent | 90ab7c4cc4edaedb9180b7ec73570c0cc06fa0ab (diff) |
Fixed faulty logic from last commit
-rw-r--r-- | editmode.c | 16 |
1 files changed, 6 insertions, 10 deletions
@@ -194,21 +194,17 @@ int new_line(char filename[], long long new_line_pos_temp, uint64_t amount_of_li size_t len = strlen(line); - // Get length of line - // Get length of amount of newlines - // put them both in a buffer - // Input the buffer into write_line - size_t total_size = amount_of_lines+len+2; - char *to_be_written = malloc(total_size); + size_t total_size = (size_t)amount_of_lines+len+1; // Calculate total size + char *to_be_written = malloc(total_size); // Allocate memory with total_size size uint64_t i = 0; for ( ; i < amount_of_lines ; i++) { - to_be_written[i] = '\n'; + to_be_written[i] = '\n'; // Insert newlines we wanna input } - to_be_written[i+1] = '\0'; + to_be_written[i] = '\0'; // Insert null character so strcat works properly - strcat(to_be_written, line); + strcat(to_be_written, line); // Write actual line contents after all the newlines - write_line(filename, new_line_pos+1, to_be_written, total_size-1); + write_line(filename, new_line_pos+1, to_be_written, total_size-1); // Actually write the changes free(line); free(to_be_written); } |