--- libgcc2.c-original Tue Mar 20 19:22:16 2001 +++ libgcc2.c Wed Mar 21 12:33:57 2001 @@ -1459,6 +1459,10 @@ char *ctime (); #include "gcov-io.h" #include +/* For O_RDWR and O_CREAT */ +#include +#include + static struct bb *bb_head; /* Return the number of digits needed to print a value */ @@ -1478,6 +1482,23 @@ static struct bb *bb_head; return ret; } +/* Opens a file, locking it for exclusive access. Used for .da files + * below. If locking is not available, define it as fopen(name, + * "rw"). */ +static FILE * +lopen(const char *name) +{ + int fd = open(name, O_RDWR | O_CREAT, 0666); + if (fd < 0) + return NULL; + + while ( (lockf(fd, F_LOCK, 0) < 0)) + if (errno != EINTR) + return NULL; + + return fdopen(fd, "r+b"); +} + void __bb_exit_func (void) { @@ -1499,21 +1520,21 @@ __bb_exit_func (void) for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next) { + long n_counts = 0; + /* If the file exists, and the number of counts in it is the same, then merge them in. */ - - if ((da_file = fopen (ptr->filename, "r")) != 0) - { - long n_counts = 0; - - if (__read_long (&n_counts, da_file, 8) != 0) - { - fprintf (stderr, "arc profiling: Can't read output file %s.\n", - ptr->filename); - continue; - } - if (n_counts == ptr->ncounts) + + if ((da_file = lopen (ptr->filename)) == 0) + { + fprintf (stderr, "arc profiling: Can't open output file %s.\n", + ptr->filename); + continue; + } + + if ( (__read_long (&n_counts, da_file, 8) == 0) + && (n_counts == ptr->ncounts) ) { int i; @@ -1530,17 +1551,10 @@ __bb_exit_func (void) ptr->counts[i] += v; } } - - if (fclose (da_file) == EOF) - fprintf (stderr, "arc profiling: Error closing output file %s.\n", - ptr->filename); - } - if ((da_file = fopen (ptr->filename, "w")) == 0) - { - fprintf (stderr, "arc profiling: Can't open output file %s.\n", - ptr->filename); - continue; - } + + /* Perhaps we should also truncate the file before + * writing? */ + rewind(da_file); /* ??? Should first write a header to the file. Preferably, a 4 byte magic number, 4 bytes containing the time the program was