switch to new util file functions

Replace all fopen(), rename(), and unlink() calls with the new util
functions.
This commit is contained in:
Miroslav Lichvar
2019-10-22 18:06:25 +02:00
parent 7dfd4ae556
commit e18903a6b5
10 changed files with 49 additions and 149 deletions

View File

@@ -203,7 +203,7 @@ REF_Initialise(void)
/* Now see if we can get the drift file opened */
drift_file = CNF_GetDriftFile();
if (drift_file) {
in = fopen(drift_file, "r");
in = UTI_OpenFile(NULL, drift_file, NULL, 'r', 0);
if (in) {
if (fscanf(in, "%lf%lf", &file_freq_ppm, &file_skew_ppm) == 2) {
/* We have read valid data */
@@ -336,50 +336,20 @@ REF_GetLeapMode(void)
static void
update_drift_file(double freq_ppm, double skew)
{
char *temp_drift_file;
FILE *out;
int r1, r2;
/* Create a temporary file with a '.tmp' extension. */
temp_drift_file = (char*) Malloc(strlen(drift_file)+8);
if(!temp_drift_file) {
out = UTI_OpenFile(NULL, drift_file, ".tmp", 'w', 0644);
if (!out)
return;
}
strcpy(temp_drift_file,drift_file);
strcat(temp_drift_file,".tmp");
out = fopen(temp_drift_file, "w");
if (!out) {
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not open temporary driftfile %s.tmp for writing",
drift_file);
return;
}
/* Write the frequency and skew parameters in ppm */
r1 = fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
r2 = fclose(out);
if (r1 < 0 || r2) {
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not write to temporary driftfile %s.tmp",
drift_file);
return;
}
fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
fclose(out);
/* Rename the temporary file to the correct location (see rename(2) for details). */
if (rename(temp_drift_file,drift_file)) {
unlink(temp_drift_file);
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not replace old driftfile %s with new one %s.tmp",
drift_file,drift_file);
return;
}
Free(temp_drift_file);
/* Rename the temporary file to the correct location */
if (!UTI_RenameTempFile(NULL, drift_file, ".tmp", NULL))
;
}
/* ================================================== */