1616#include <sys/stat.h>
1717#include <errno.h>
1818#include <sys/file.h>
19+ #include <limits.h>
1920
2021#define System_wisdom_file "/etc/fftw/wisdomf"
21- #define Wisdom_dir "/var/lib/ka9q-radio"
22- #define Wisdom_file "/var/lib/ka9q-radio/wisdom"
22+
23+ #ifndef STATEDIR
24+ #define STATEDIR "/var/lib/ka9q-radio"
25+ #endif
26+ #define WISDOM_FILE "wisdom"
2327
2428static int Verbose ;
2529static bool Force ;
@@ -173,16 +177,18 @@ int main(int argc,char *argv[]){
173177 }
174178 }
175179 track_wisdom_length ();
176- lr = fftwf_import_wisdom_from_filename (Wisdom_file );
180+ char wisdom_file [PATH_MAX ];
181+ snprintf (wisdom_file ,sizeof wisdom_file ,"%s/%s" ,STATEDIR ,WISDOM_FILE );
182+ lr = fftwf_import_wisdom_from_filename (wisdom_file );
177183 if (Verbose > 1 ){
178- printf ("fftwf_import_wisdom_from_filename(%s) %s\n" ,Wisdom_file ,lr ? "succeeded" : "failed" );
184+ printf ("fftwf_import_wisdom_from_filename(%s) %s\n" ,wisdom_file ,lr ? "succeeded" : "failed" );
179185 if (!lr ){
180- if (access (Wisdom_file ,R_OK ) == -1 ){
181- printf ("%s not readable: %s\n" ,Wisdom_file ,strerror (errno ));
186+ if (access (wisdom_file ,R_OK ) == -1 ){
187+ printf ("%s not readable: %s\n" ,wisdom_file ,strerror (errno ));
182188 }
183189 }
184- if (access (Wisdom_file ,W_OK ) == -1 )
185- printf ("Warning: %s not writeable, exports will fail: %s\n" ,Wisdom_file ,strerror (errno ));
190+ if (access (wisdom_file ,W_OK ) == -1 )
191+ printf ("Warning: %s not writeable, exports will fail: %s\n" ,wisdom_file ,strerror (errno ));
186192 }
187193 track_wisdom_length ();
188194 }
@@ -310,7 +316,10 @@ static int save_plans(){
310316 int fd = -1 ;
311317
312318 // Import or re-import wisdom and merge
313- if (asprintf (& lockfile ,"%s.lock" ,Wisdom_file ) <= 0 ){
319+ char wisdom_file [PATH_MAX ];
320+ snprintf (wisdom_file ,sizeof wisdom_file ,"%s/%s" ,STATEDIR ,WISDOM_FILE );
321+
322+ if (asprintf (& lockfile ,"%s.lock" ,wisdom_file ) <= 0 ){
314323 free (lockfile );
315324 return 0 ;
316325 }
@@ -320,12 +329,12 @@ static int save_plans(){
320329 printf ("Can't acquire lock on %s\n" ,lockfile );
321330 else
322331 flock (lockfd ,LOCK_EX );
323- bool reimport = fftwf_import_wisdom_from_filename (Wisdom_file );
332+ bool reimport = fftwf_import_wisdom_from_filename (wisdom_file );
324333 if (Verbose > 1 )
325- printf ("fftwf_import_wisdom_from_filename(%s) %s\n" ,Wisdom_file ,reimport ? "succeeded" : "failed" );
334+ printf ("fftwf_import_wisdom_from_filename(%s) %s\n" ,wisdom_file ,reimport ? "succeeded" : "failed" );
326335 track_wisdom_length ();
327336
328- if (asprintf (& newtemp ,"%s-XXXXXX" ,Wisdom_file ) < 0 )
337+ if (asprintf (& newtemp ,"%s-XXXXXX" ,wisdom_file ) < 0 )
329338 goto quit ;
330339
331340 wisdom = fftwf_export_wisdom_to_string ();
@@ -350,7 +359,7 @@ static int save_plans(){
350359
351360 // Copy user/group/mode from old version
352361 struct stat st ;
353- if (stat (Wisdom_file ,& st ) == 0 ){
362+ if (stat (wisdom_file ,& st ) == 0 ){
354363 int r = fchown (fd , st .st_uid , st .st_gid ); // Best effort
355364 (void )r ;
356365 fchmod (fd , st .st_mode );
@@ -363,13 +372,13 @@ static int save_plans(){
363372 fsync (fd );
364373 close (fd );
365374 fd = -1 ;
366- int dir = open (Wisdom_dir ,O_DIRECTORY );
375+ int dir = open (STATEDIR ,O_DIRECTORY );
367376 fsync (dir );
368377 close (dir );
369- if (rename (newtemp ,Wisdom_file ) != 0 )
370- printf ("rename %s to %s failed: %s\n" ,newtemp ,Wisdom_file ,strerror (errno ));
378+ if (rename (newtemp ,wisdom_file ) != 0 )
379+ printf ("rename %s to %s failed: %s\n" ,newtemp ,wisdom_file ,strerror (errno ));
371380 else if (Verbose > 1 )
372- printf ("rename %s to %s succeeded\n" ,newtemp ,Wisdom_file );
381+ printf ("rename %s to %s succeeded\n" ,newtemp ,wisdom_file );
373382
374383 if (Force )
375384 fftwf_forget_wisdom (); // start fresh for the next on the list
0 commit comments