gengtype.c (is_file_equal): New function.
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. Co-Authored-By: Rafael Avila de Espindola <espindola@google.com> From-SVN: r152049
This commit is contained in:
parent
0182d01649
commit
41e7ac516e
|
@ -1,3 +1,9 @@
|
||||||
|
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.
|
||||||
|
|
||||||
2009-09-22 Basile Starynkevitch <basile@starynkevitch.net>
|
2009-09-22 Basile Starynkevitch <basile@starynkevitch.net>
|
||||||
Rafael Avila de Espindola <espindola@google.com>
|
Rafael Avila de Espindola <espindola@google.com>
|
||||||
|
|
||||||
|
|
|
@ -1794,6 +1794,32 @@ get_output_file_name (const char *input_file)
|
||||||
return NULL;
|
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,
|
/* Copy the output to its final destination,
|
||||||
but don't unnecessarily change modification times. */
|
but don't unnecessarily change modification times. */
|
||||||
|
|
||||||
|
@ -1804,35 +1830,20 @@ close_output_files (void)
|
||||||
|
|
||||||
for (of = output_files; of; of = of->next)
|
for (of = output_files; of; of = of->next)
|
||||||
{
|
{
|
||||||
FILE * newfile;
|
|
||||||
|
|
||||||
newfile = fopen (of->name, "r");
|
if (!is_file_equal(of))
|
||||||
if (newfile != NULL )
|
{
|
||||||
{
|
FILE *newfile = fopen (of->name, "w");
|
||||||
int no_write_p;
|
if (newfile == NULL)
|
||||||
size_t i;
|
fatal ("opening output file %s: %s", of->name, strerror (errno));
|
||||||
|
if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
|
||||||
for (i = 0; i < of->bufused; i++)
|
fatal ("writing output file %s: %s", of->name, strerror (errno));
|
||||||
{
|
if (fclose (newfile) != 0)
|
||||||
int ch;
|
fatal ("closing output file %s: %s", of->name, strerror (errno));
|
||||||
ch = fgetc (newfile);
|
}
|
||||||
if (ch == EOF || ch != (unsigned char) of->buf[i])
|
free(of->buf);
|
||||||
break;
|
of->buf = NULL;
|
||||||
}
|
of->bufused = of->buflength = 0;
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue