Close error file opened by name
authorEric Biggers <ebiggers3@gmail.com>
Wed, 25 Jun 2014 02:39:08 +0000 (21:39 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Wed, 25 Jun 2014 02:39:08 +0000 (21:39 -0500)
src/util.c
src/wim.c

index 86fcb05983f3308bf22241bda121e7f75e40857a..3bb78efb6a553036905b40d418885f5d1a6d9e2a 100644 (file)
@@ -67,6 +67,7 @@ utf16le_strlen(const utf16lechar *s)
 #ifdef ENABLE_ERROR_MESSAGES
 bool wimlib_print_errors = false;
 FILE *wimlib_error_file = NULL; /* Set in wimlib_global_init() */
+static bool wimlib_owns_error_file = false;
 #endif
 
 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
@@ -192,8 +193,11 @@ WIMLIBAPI int
 wimlib_set_error_file(FILE *fp)
 {
 #ifdef ENABLE_ERROR_MESSAGES
+       if (wimlib_owns_error_file)
+               fclose(wimlib_error_file);
        wimlib_error_file = fp;
-       wimlib_print_errors = true;
+       wimlib_print_errors = (fp != NULL);
+       wimlib_owns_error_file = false;
        return 0;
 #else
        return WIMLIB_ERR_UNSUPPORTED;
@@ -209,9 +213,8 @@ wimlib_set_error_file_by_name(const char *path)
        fp = fopen(path, "a");
        if (!fp)
                return WIMLIB_ERR_OPEN;
-
-       wimlib_error_file = fp;
-       wimlib_print_errors = true;
+       wimlib_set_error_file(fp);
+       wimlib_owns_error_file = true;
        return 0;
 #else
        return WIMLIB_ERR_UNSUPPORTED;
index 38d9071886c28f09862286fa34b7e75059e1a250..fc15a21db00c6a541091e770a29ca7eef8863260 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -1071,5 +1071,7 @@ wimlib_global_cleanup(void)
 #endif
        cleanup_decompressor_params();
        cleanup_compressor_params();
+
+       wimlib_set_error_file(NULL);
        lib_initialized = false;
 }