From ae7f5ebdeb7f544bd9dff33fba5553bab91c90a4 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 5 Jun 2020 17:08:04 +0200 Subject: [PATCH] tweaks: move a function to before the one that calls it --- src/nano.c | 73 ++++++++++++++++++++++++++--------------------------- src/proto.h | 1 - 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/nano.c b/src/nano.c index 6927ac80..d69ac505 100644 --- a/src/nano.c +++ b/src/nano.c @@ -273,6 +273,42 @@ void finish(void) exit(0); } +/* Save the current buffer under the given name (or under the name "nano" + * for a nameless buffer). If needed, the name is modified to be unique. */ +void emergency_save(const char *die_filename, struct stat *die_stat) +{ + bool failed = TRUE; + char *targetname; + + if (*die_filename == '\0') + die_filename = "nano"; + + targetname = get_next_filename(die_filename, ".save"); + + if (*targetname != '\0') + failed = !write_file(targetname, NULL, TRUE, OVERWRITE, FALSE); + + if (!failed) + fprintf(stderr, _("\nBuffer written to %s\n"), targetname); + else if (*targetname != '\0') + fprintf(stderr, _("\nBuffer not written to %s: %s\n"), + targetname, strerror(errno)); + else + fprintf(stderr, _("\nToo many .save files")); + +#ifndef NANO_TINY + /* Try to chmod/chown the saved file to the values of the original file, + * but ignore any failure as we are in a hurry to get out. */ + if (die_stat) { + IGNORE_CALL_RESULT(chmod(targetname, die_stat->st_mode)); + IGNORE_CALL_RESULT(chown(targetname, die_stat->st_uid, + die_stat->st_gid)); + } +#endif + + free(targetname); +} + /* Die gracefully -- by restoring the terminal state and saving any buffers * that were modified. */ void die(const char *msg, ...) @@ -313,43 +349,6 @@ void die(const char *msg, ...) exit(1); } -/* Save the current buffer under the given name. - * If necessary, the name is modified to be unique. */ -void emergency_save(const char *die_filename, struct stat *die_stat) -{ - char *targetname; - bool failed = TRUE; - - /* If the buffer has no name, simply call it "nano". */ - if (*die_filename == '\0') - die_filename = "nano"; - - targetname = get_next_filename(die_filename, ".save"); - - if (*targetname != '\0') - failed = !write_file(targetname, NULL, TRUE, OVERWRITE, FALSE); - - if (!failed) - fprintf(stderr, _("\nBuffer written to %s\n"), targetname); - else if (*targetname != '\0') - fprintf(stderr, _("\nBuffer not written to %s: %s\n"), - targetname, strerror(errno)); - else - fprintf(stderr, _("\nToo many .save files")); - -#ifndef NANO_TINY - /* Try to chmod/chown the saved file to the values of the original file, - * but ignore any failure as we are in a hurry to get out. */ - if (die_stat) { - IGNORE_CALL_RESULT(chmod(targetname, die_stat->st_mode)); - IGNORE_CALL_RESULT(chown(targetname, die_stat->st_uid, - die_stat->st_gid)); - } -#endif - - free(targetname); -} - /* Initialize the three window portions nano uses. */ void window_init(void) { diff --git a/src/proto.h b/src/proto.h index 7d35c52b..19f9d635 100644 --- a/src/proto.h +++ b/src/proto.h @@ -411,7 +411,6 @@ void say_there_is_no_help(void); #endif void finish(void); void die(const char *msg, ...); -void emergency_save(const char *die_filename, struct stat *die_stat); void window_init(void); void do_exit(void); void close_and_go(void);