summaryrefslogtreecommitdiff
path: root/gcc/gengtype.c
diff options
context:
space:
mode:
authorespindola <espindola@138bc75d-0d04-0410-961f-82ee72b054a4>2009-09-22 21:36:50 +0000
committerespindola <espindola@138bc75d-0d04-0410-961f-82ee72b054a4>2009-09-22 21:36:50 +0000
commit8797d68410f34c33f0f66effb5936761d6d5c99e (patch)
treeaf45ed3a0b306ce509214b3f296ebd22cb64e789 /gcc/gengtype.c
parent8603e6a72352c36ff16b063c3a0ffc42c45e39e6 (diff)
downloadgcc-8797d68410f34c33f0f66effb5936761d6d5c99e.tar.gz
2009-09-22 Basile Starynkevitch <basile@starynkevitch.net>
Rafael Avila de Espindola <espindola@google.com> * gengtype.c (is_file_equal): New function. (close_output_files): Use is_file_equal. Free of->buf. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@152049 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/gengtype.c')
-rw-r--r--gcc/gengtype.c67
1 files changed, 39 insertions, 28 deletions
diff --git a/gcc/gengtype.c b/gcc/gengtype.c
index 0ee9c48050a..7d7f9d190f3 100644
--- a/gcc/gengtype.c
+++ b/gcc/gengtype.c
@@ -1794,6 +1794,32 @@ get_output_file_name (const char *input_file)
return NULL;
}
+/* Check if existing file is equal to the in memory buffer. */
+
+static bool
+is_file_equal (outf_p of)
+{
+ FILE *newfile = fopen (of->name, "r");
+ size_t i;
+ bool equal;
+ if (newfile == NULL)
+ return false;
+
+ equal = true;
+ for (i = 0; i < of->bufused; i++)
+ {
+ int ch;
+ ch = fgetc (newfile);
+ if (ch == EOF || ch != (unsigned char) of->buf[i])
+ {
+ equal = false;
+ break;
+ }
+ }
+ fclose (newfile);
+ return equal;
+}
+
/* Copy the output to its final destination,
but don't unnecessarily change modification times. */
@@ -1804,35 +1830,20 @@ close_output_files (void)
for (of = output_files; of; of = of->next)
{
- FILE * newfile;
- newfile = fopen (of->name, "r");
- if (newfile != NULL )
- {
- int no_write_p;
- size_t i;
-
- for (i = 0; i < of->bufused; i++)
- {
- int ch;
- ch = fgetc (newfile);
- if (ch == EOF || ch != (unsigned char) of->buf[i])
- break;
- }
- no_write_p = i == of->bufused && fgetc (newfile) == EOF;
- fclose (newfile);
-
- if (no_write_p)
- continue;
- }
-
- newfile = fopen (of->name, "w");
- if (newfile == NULL)
- fatal ("opening output file %s: %s", of->name, strerror (errno));
- if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
- fatal ("writing output file %s: %s", of->name, strerror (errno));
- if (fclose (newfile) != 0)
- fatal ("closing output file %s: %s", of->name, strerror (errno));
+ if (!is_file_equal(of))
+ {
+ FILE *newfile = fopen (of->name, "w");
+ if (newfile == NULL)
+ fatal ("opening output file %s: %s", of->name, strerror (errno));
+ if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
+ fatal ("writing output file %s: %s", of->name, strerror (errno));
+ if (fclose (newfile) != 0)
+ fatal ("closing output file %s: %s", of->name, strerror (errno));
+ }
+ free(of->buf);
+ of->buf = NULL;
+ of->bufused = of->buflength = 0;
}
}