X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Ftemp-file.c;h=839b99e7572bb18a1cf634a4b54da0925155ee65;hb=92c31025565a5b5c270c83b0208ab46a90e9b64b;hp=0391ec9c01f91ff1f3384e293c2d9b41102dabc0;hpb=228b83840e3f053aecfa9feac395fb6855adf044;p=pspp diff --git a/src/libpspp/temp-file.c b/src/libpspp/temp-file.c index 0391ec9c01..839b99e757 100644 --- a/src/libpspp/temp-file.c +++ b/src/libpspp/temp-file.c @@ -40,6 +40,7 @@ */ +static void cleanup (void); static struct temp_dir *temp_dir; struct hmapx map; @@ -51,11 +52,35 @@ setup (void) temp_dir = create_temp_dir ("pspp", NULL, true); } +static void +initialise (void) +{ + if (temp_dir == NULL) + { + setup (); + if (temp_dir == NULL) + return ; + atexit (cleanup); + } +} + + +const char * +temp_dir_name (void) +{ + initialise (); + + if (temp_dir) + return temp_dir->dir_name; + + return NULL; +} + static void cleanup (void) { struct hmapx_node *node; - const char *fn; + char *fn; cleanup_temp_dir (temp_dir); @@ -74,13 +99,9 @@ create_temp_file (void) char *file_name; FILE *stream; + initialise (); if (temp_dir == NULL) - { - setup (); - if (temp_dir == NULL) - return NULL; - atexit (cleanup); - } + return NULL; file_name = xasprintf ("%s/%d", temp_dir->dir_name, idx++); register_temp_file (temp_dir, file_name); @@ -104,7 +125,7 @@ close_temp_file (FILE *file) struct hmapx_node *node = hmapx_first_with_hash (&map, hash_pointer (file, 0)); char *fn = node->data; fclose_temp (file); - cleanup_temp_file (temp_dir, fn); + cleanup_temp_file (temp_dir, fn); hmapx_delete (&map, node); free (fn); }