From 23f73cb8b3d90dd44c14771838588a554af77c5c Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 1 Apr 2020 16:05:45 +0200 Subject: [PATCH] tweaks: exit from the writing loop as soon as the last line is reached Avoid having to decrement the count in order to offset the increment at the end of the loop. Also, declare a variable on a separate line, and rename it. --- src/files.c | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/files.c b/src/files.c index e1952c2c..d22489a5 100644 --- a/src/files.c +++ b/src/files.c @@ -1873,18 +1873,19 @@ bool write_file(const char *name, FILE *thefile, bool tmp, if (!tmp) statusbar(_("Writing...")); - while (line != NULL) { - size_t data_len = strlen(line->data), size; + while (TRUE) { + size_t data_len = strlen(line->data); + size_t wrote; /* Decode LFs as the NULs that they are, before writing to disk. */ sunder(line->data); - size = fwrite(line->data, sizeof(char), data_len, thefile); + wrote = fwrite(line->data, sizeof(char), data_len, thefile); /* Re-encode any embedded NULs as LFs. */ unsunder(line->data, data_len); - if (size < data_len) { + if (wrote < data_len) { statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); fclose(thefile); goto cleanup_and_exit; @@ -1894,27 +1895,28 @@ bool write_file(const char *name, FILE *thefile, bool tmp, * character after it. If this last line is empty, it means zero bytes * are written for it, and we don't count it in the number of lines. */ if (line->next == NULL) { - if (line->data[0] == '\0') - lineswritten--; - } else { -#ifndef NANO_TINY - if (openfile->fmt == DOS_FILE || openfile->fmt == MAC_FILE) { - if (putc('\r', thefile) == EOF) { - statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); - fclose(thefile); - goto cleanup_and_exit; - } - } - - if (openfile->fmt != MAC_FILE) -#endif - if (putc('\n', thefile) == EOF) { - statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); - fclose(thefile); - goto cleanup_and_exit; - } + if (line->data[0] != '\0') + lineswritten++; + break; } +#ifndef NANO_TINY + if (openfile->fmt == DOS_FILE || openfile->fmt == MAC_FILE) { + if (putc('\r', thefile) == EOF) { + statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); + fclose(thefile); + goto cleanup_and_exit; + } + } + + if (openfile->fmt != MAC_FILE) +#endif + if (putc('\n', thefile) == EOF) { + statusline(ALERT, _("Error writing %s: %s"), realname, strerror(errno)); + fclose(thefile); + goto cleanup_and_exit; + } + line = line->next; lineswritten++; }