--- indent-2.2.9/src/output.h.bak 2002-08-04 20:08:41.000000000 +0300 +++ indent-2.2.9/src/output.h 2007-09-05 01:19:47.946316254 +0300 @@ -40,14 +40,15 @@ extern void open_output( const char * filename, const char * mode); -extern void reopen_output_trunc( - const char * filename); +extern void create_output_temp( + const char * filename, + char *tmpfn /* must be >= 4096 bytes */ ); extern void close_output( struct stat * file_stats, const char * filename); -extern inhibit_indenting( +extern void inhibit_indenting( BOOLEAN flag); --- indent-2.2.9/src/output.c.bak 2007-09-05 00:45:22.569650000 +0300 +++ indent-2.2.9/src/output.c 2007-09-05 01:27:15.264941310 +0300 @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "indent.h" #include "sys.h" @@ -1121,7 +1123,8 @@ extern void dump_line ( extern void flush_output(void) { - fflush (output); + if (fflush (output) == EOF) + fatal(_("Error writing output file: %s"), strerror(errno)); } /* @@ -1162,11 +1165,32 @@ void open_output( * History: */ -extern void reopen_output_trunc( - const char * filename) +extern void create_output_temp( + const char * filename, + char* tmpfn) { - output = freopen(filename, "w", output); + int fd; + + if (snprintf(tmpfn, 4096, "%s.tmp.XXXXXX", filename) >= 4096) + { + fatal(_("too long filename: %s"), tmpfn); + } + + fd = mkstemp(tmpfn); + if (fd == -1) + { + fatal(_("mkstemp failed: %s"), strerror(errno)); + } + if (output) + { + fclose(output); + } + output = fdopen(fd, "w"); + if (output == NULL) + { + fatal(_("fdopen failed: %s"), strerror(errno)); + } } /* @@ -1184,6 +1208,11 @@ extern void close_output( { if (output != stdout) { + flush_output(); + if (fsync(fileno(output)) == -1 && (errno != EINVAL)) + { + fatal(_("Can't fsync output file %s"), filename); + } if (fclose(output) != 0) { fatal(_("Can't close output file %s"), filename); --- indent-2.2.9/src/backup.c.bak 2002-08-04 20:08:41.000000000 +0300 +++ indent-2.2.9/src/backup.c 2007-09-05 01:05:59.401206744 +0300 @@ -51,6 +51,7 @@ #include "sys.h" #include #include +#include #ifdef HAVE_UNISTD_H #include @@ -421,6 +422,8 @@ void make_backup ( FILE * bf; char * backup_filename; unsigned int size; + char tmpfn[4096]; + int fd; if (version_control == none) { @@ -435,22 +438,40 @@ void make_backup ( exit (system_error); } - bf = fopen (backup_filename, "w"); - + if (snprintf(tmpfn, sizeof(tmpfn), "%s.tmp.XXXXXX", backup_filename) >= sizeof(tmpfn)) + { + fatal (_("Wacky backupfile %s"), tmpfn); + } + + fd = mkstemp(tmpfn); + if (fd == -1) + { + fatal (_("mkstemp for backupfile failed: %s"), strerror(errno)); + } + + bf = fdopen (fd, "w"); if (!bf) { - fatal (_("Can't open backup file %s"), backup_filename); + fatal (_("Can't open backup file %s"), tmpfn); } size = fwrite (file->data, file->size, 1, bf); - if (size != 1) + if ((size != 1) || (fflush(bf) == EOF)) { - fatal (_("Can't write to backup file %s"), backup_filename); + fatal (_("Can't write to backup file %s"), tmpfn); } + if (fsync(fileno(bf)) == -1 && (errno != EINVAL)) + { + fatal (_("Can't fsync backup file %s"), tmpfn); + } + + if (fclose (bf) == EOF) + { + fatal (_("Can't close backup file %s"), tmpfn); + } - fclose (bf); #ifdef PRESERVE_MTIME { struct utimbuf buf; @@ -458,12 +479,17 @@ void make_backup ( buf.actime = time (NULL); buf.modtime = file_stats->st_mtime; - if (utime (backup_filename, &buf) != 0) + if (utime (tmpfn, &buf) != 0) { - WARNING (_("Can't preserve modification time on backup file %s"), backup_filename, 0); + WARNING (_("Can't preserve modification time on backup file %s"), tmpfn, 0); } } #endif - + + if (rename(tmpfn, backup_filename) == -1) + { + fatal(_("Can't rename temp filename to backupfilename: %s"), strerror(errno)); + } + free (backup_filename); } --- indent-2.2.9/src/indent.c.bak 2002-10-28 22:00:56.000000000 +0200 +++ indent-2.2.9/src/indent.c 2007-09-05 01:26:14.843642800 +0300 @@ -29,6 +29,7 @@ #include "sys.h" +#include #if defined (HAVE_UNISTD_H) #include #endif @@ -373,8 +374,6 @@ static void handle_token_lparen( BOOLEAN * sp_sw, int * dec_ind) { - char lparen = *token; - /* Braces in initializer lists should be put on new lines. This is * necessary so that -gnu does not cause things like char * *this_is_a_string_array[] = { "foo", "this_string_does_not_fit", @@ -875,6 +874,7 @@ static void handle_token_colon( * imply we are in a stmt */ for (t_ptr = s_code; *t_ptr; ++t_ptr) { + check_lab_size(); *e_lab++ = *t_ptr; /* turn everything so far into a label */ } @@ -977,13 +977,10 @@ static void handle_token_semicolon( if (!*sp_sw) { /* if not if for (;;) */ - do - { if (parse (semicolon) != total_success) { *file_exit_value = indent_error; } - } while(0); *force_nl = true; /* force newline after a end of stmt */ } @@ -2626,6 +2623,18 @@ static exit_values_ty indent_main_loop(v return file_exit_value; /* RETURN */ } + if (type_code == sp_paren + && parser_state_tos->p_stack[parser_state_tos->tos] == dohead + && parser_state_tos->last_token == rbrace) + { + /* This is closing `while' of `do {stuff;} while' + statement (not `do stuff; while' command). In -cdw, we + want to suppress newline. */ + if (settings.cuddle_do_while) + force_nl = false; + parser_state_tos->in_closing_br_while = true; + } + if ((type_code != comment) && (type_code != cplus_comment) && (type_code != newline) && @@ -2933,6 +2942,7 @@ static exit_values_ty process_args ( /******************************************************************************/ static exit_values_ty indent_multiple_files(void) { + char tmpfn[4096]; exit_values_ty exit_status = total_success; int i; @@ -2953,9 +2963,8 @@ static exit_values_ty indent_multiple_fi make_backup(current_input, &file_stats); /* Aborts on failure. */ /* We have safely made a backup so the open file can be truncated. */ - - reopen_output_trunc(out_name); - + create_output_temp(out_name, tmpfn); + reset_parser(); status = indent (current_input); @@ -2972,6 +2981,10 @@ static exit_values_ty indent_multiple_fi { close_output(NULL, out_name); } + if (rename(tmpfn, out_name) == -1) + { + fatal (_("rename failed: %s"), strerror(errno)); + } } return exit_status; @@ -2982,6 +2995,8 @@ static exit_values_ty indent_single_file { exit_values_ty exit_status = total_success; struct stat file_stats; + char tmpfn[4096]; + int madetemp = 0; if (input_files == 0 || using_stdin) { @@ -3013,7 +3028,8 @@ static exit_values_ty indent_single_file } else { - open_output(out_name, "w"); + create_output_temp(out_name, tmpfn); + madetemp = 1; } reset_parser (); @@ -3027,6 +3043,10 @@ static exit_values_ty indent_single_file else { close_output(NULL, out_name); + if (madetemp && rename(tmpfn, out_name) == -1) + { + fatal(_("rename failed: %s"), strerror(errno)); + } } return exit_status; @@ -3057,13 +3077,12 @@ int main ( int argc, char ** argv) { - int i; char *profile_pathname = 0; BOOLEAN using_stdin = false; exit_values_ty exit_status; #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) - setlocale (LC_MESSAGES, ""); + setlocale (LC_ALL, ""); #endif bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE);