diff --git a/lib/utils.c b/lib/utils.c
index 2d0390e710c0a8e9bd86e89e60d6dda5b1c224c9..5f3c6035537dd4404ec347f466f48f33145fb6f6 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -23,6 +23,11 @@ void _cleanup_close(int *p)
     if (*p > 0) close(*p);
 }
 
+void _cleanup_fclose(FILE **p)
+{
+    if (*p) fclose(*p);
+}
+
 char* kr_strcatdup(unsigned n, ...)
 {
     /* Calculate total length */
diff --git a/lib/utils.h b/lib/utils.h
index 3d6a57cc51cd4a011a3edcb9fbd677aea42fe215..1feaa1dc3267509f2cba9867b8f1c88a0b8f2d25 100644
--- a/lib/utils.h
+++ b/lib/utils.h
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <stdio.h>
+
 /*
  * General-purpose attributes.
  */
@@ -23,6 +25,8 @@
 extern void _cleanup_free(char **p);
 #define auto_close __attribute__((cleanup(_cleanup_close)))
 extern void _cleanup_close(int *p);
+#define auto_fclose __attribute__((cleanup(_cleanup_fclose)))
+extern void _cleanup_fclose(FILE **p);
 
 /*
  * Defines.