summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2016-12-09 09:22:29 +0100
committerThomas Haller <thaller@redhat.com>2016-12-13 11:26:58 +0100
commit312cea870dfbc363da44074bd6f56ccd283c5420 (patch)
tree068eccf5ca36324e23a301b6996eb470d9e81505
parent7744fdd00f9bb32643cd02f81becf34926e2f1c7 (diff)
downloadNetworkManager-312cea870dfbc363da44074bd6f56ccd283c5420.tar.gz
shared: add nm_auto_close and nm_auto_fclose
We already have gs_fd_close, which however doesn't preserve errno and only checks for fd != -1. Add our own define. Downside is, we have to include stdio.h and errno.h, which effectively ends up to be included *everywhere*.
-rw-r--r--shared/nm-utils/nm-macros-internal.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index fdb45f9291..9fb62b447c 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -22,7 +22,9 @@
#ifndef __NM_MACROS_INTERNAL_H__
#define __NM_MACROS_INTERNAL_H__
+#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
#include "nm-glib.h"
@@ -59,6 +61,30 @@ _nm_auto_free_gstring_impl (GString **str)
}
#define nm_auto_free_gstring nm_auto(_nm_auto_free_gstring_impl)
+static inline void
+_nm_auto_close_impl (int *pfd)
+{
+ if (*pfd >= 0) {
+ int errsv = errno;
+
+ (void) close (*pfd);
+ errno = errsv;
+ }
+}
+#define nm_auto_close nm_auto(_nm_auto_close_impl)
+
+static inline void
+_nm_auto_fclose_impl (FILE **pfd)
+{
+ if (*pfd) {
+ int errsv = errno;
+
+ (void) fclose (*pfd);
+ errno = errsv;
+ }
+}
+#define nm_auto_fclose nm_auto(_nm_auto_fclose_impl)
+
/*****************************************************************************/
/* http://stackoverflow.com/a/11172679 */