Index: src/files.c =================================================================== --- src/files.c (revision 5604) +++ src/files.c (working copy) @@ -383,6 +383,24 @@ return retval; } + +/* If *pstat is NULL, perform a stat call with the given file name. On + * success, *pstat points to a newly allocated buffer containing the stat's + * result. On stat's failure, the NULL pointer in *pstat is left intact. */ +void stat_if_needed(const char *filename, struct stat **pstat) +{ + struct stat *tmp; + + if (*pstat != NULL) + return; + + tmp = (struct stat *)nmalloc(sizeof(struct stat)); + + if (stat(filename, tmp) == 0) + *pstat = tmp; + else + free(tmp); +} #endif /* !NANO_TINY */ /* If it's not "", filename is a file to open. We make a new buffer, if @@ -466,11 +484,7 @@ if (rc > 0) { read_file(f, rc, filename, undoable, new_buffer); #ifndef NANO_TINY - if (openfile->current_stat == NULL) { - openfile->current_stat = - (struct stat *)nmalloc(sizeof(struct stat)); - stat(filename, openfile->current_stat); - } + stat_if_needed(filename, &openfile->current_stat); #endif } @@ -1821,10 +1835,8 @@ * specified it interactively), stat and save the value now, * or else we will chase null pointers when we do modtime checks, * preserve file times, and so on, during backup. */ - if (openfile->current_stat == NULL && !tmp && realexists) { - openfile->current_stat = (struct stat *)nmalloc(sizeof(struct stat)); - stat(realname, openfile->current_stat); - } + if (!tmp && realexists) + stat_if_needed(realname, &openfile->current_stat); /* We backup only if the backup toggle is set, the file isn't * temporary, and the file already exists. Furthermore, if we @@ -2210,12 +2222,17 @@ } #ifndef NANO_TINY - /* Update current_stat to reference the file as it is now. */ - if (openfile->current_stat == NULL) - openfile->current_stat = - (struct stat *)nmalloc(sizeof(struct stat)); - if (!openfile->mark_set) - stat(realname, openfile->current_stat); + if (!openfile->mark_set) { + if (openfile->current_stat == NULL) + openfile->current_stat = + (struct stat *)nmalloc(sizeof(struct stat)); + + /* Update the stat info to reflect the file as it is now. */ + if (stat(realname, openfile->current_stat) != 0) { + free(openfile->current_stat); + openfile->current_stat = NULL; + } + } #endif statusbar(P_("Wrote %lu line", "Wrote %lu lines",