diff --git a/ChangeLog b/ChangeLog index 02d76381..34b1340f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -38,14 +38,18 @@ CVS code - file help.c; changes to help_init(), help_line_len(), and do_help() (all moved to help.c). (DLR) - Tweak a few functions to remove the assumption that the file - always ends in a magicline. Changes to do_cut_till_end() and - do_wordlinechar_count(). (DLR) + always ends in a magicline. Changes to do_cut_till_end(), + write_file(), and do_wordlinechar_count(). (DLR) - Tweak a few functions to rely on fileage and filebot instead of NULL for their checks to detect the top or bottom of the file. Changes to cut_line(), cut_to_eol(), do_page_up(), do_page_down(), do_para_end(), do_next_word(), do_prev_word(), do_up(), do_down(), do_scroll_down(), do_right(), do_mouse(), do_gotolinecolumn(), do_delete(), and find_paragraph(). (DLR) +- files.c: + read_file() + - Remove apparently unneeded logic to handle a case where + current is NULL, since it shouldn't be NULL there. (DLR) - nano.h: - Readd MIN_EDITOR_COLS #define. (DLR) - rcfile.c: diff --git a/src/files.c b/src/files.c index c6c49416..e8c0bc7e 100644 --- a/src/files.c +++ b/src/files.c @@ -449,18 +449,12 @@ void read_file(FILE *f, const char *filename) open_buffer(""); /* Did we try to insert a file of zero bytes? */ - if (num_lines != 0) { - if (openfile->current != NULL) { - fileptr->next = openfile->current; - openfile->current->prev = fileptr; - renumber(openfile->current); - openfile->current_x = 0; - openfile->placewewant = 0; - } else if (fileptr->next == NULL) { - openfile->filebot = fileptr; - new_magicline(); - openfile->totsize--; - } + if (num_lines > 0) { + fileptr->next = openfile->current; + openfile->current->prev = fileptr; + renumber(openfile->current); + openfile->current_x = 0; + openfile->placewewant = 0; } openfile->totsize += get_totsize(openfile->fileage, @@ -1416,8 +1410,17 @@ int write_file(const char *name, FILE *f_open, bool tmp, append_type * a selection. */ assert(openfile->fileage != NULL && openfile->filebot != NULL); - while (fileptr != openfile->filebot) { - size_t data_len = strlen(fileptr->data), size; + while (fileptr != NULL) { + size_t data_len, size; + + /* If we're on the last line of the file and it's blank, skip + * over it, since the newline character we wrote after the + * next-to-last line of the file is equivalent to it. */ + if (fileptr == openfile->filebot && + openfile->filebot->data[0] == '\0') + continue; + + data_len = strlen(fileptr->data); /* Newlines to nulls, just before we write to disk. */ sunder(fileptr->data); @@ -1434,27 +1437,33 @@ int write_file(const char *name, FILE *f_open, bool tmp, append_type goto cleanup_and_exit; } + /* If we're on the last line of the file and it isn't blank, + * don't write a newline character after it. */ + if (fileptr != openfile->filebot || + openfile->filebot->data[0] == '\0') { #ifndef NANO_SMALL - if (openfile->fmt == DOS_FILE || openfile->fmt == MAC_FILE) { - if (putc('\r', f) == EOF) { - statusbar(_("Error writing %s: %s"), realname, + if (openfile->fmt == DOS_FILE || openfile->fmt == + MAC_FILE) { + if (putc('\r', f) == EOF) { + statusbar(_("Error writing %s: %s"), realname, strerror(errno)); - fclose(f); - goto cleanup_and_exit; + fclose(f); + goto cleanup_and_exit; + } } - } - if (openfile->fmt != MAC_FILE) { + if (openfile->fmt != MAC_FILE) { #endif - if (putc('\n', f) == EOF) { - statusbar(_("Error writing %s: %s"), realname, + if (putc('\n', f) == EOF) { + statusbar(_("Error writing %s: %s"), realname, strerror(errno)); - fclose(f); - goto cleanup_and_exit; - } + fclose(f); + goto cleanup_and_exit; + } #ifndef NANO_SMALL - } + } #endif + } fileptr = fileptr->next; lineswritten++;