From 99e1e4d6a19f8fc183ceaea39830c60cdeb1e778 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 11 Dec 2016 22:35:44 +0100 Subject: [PATCH] shared: add NM_AUTO_PROTECT_ERRNO Similar to systemd's PROTECT_ERRNO. The difference it, that it doesn't treat the auto-variable as internal, so it is allowed to use it. E.g. if (!(fd = open (...)) { NM_AUTO_PROTECT_ERRNO (errno_saved); printf ("error: %s", g_strerror (errno_saved)); return FALSE; } --- shared/nm-utils/nm-macros-internal.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 9fb62b447..24499172c 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -85,6 +85,13 @@ _nm_auto_fclose_impl (FILE **pfd) } #define nm_auto_fclose nm_auto(_nm_auto_fclose_impl) +static inline void +_nm_auto_protect_errno (int *p_saved_errno) +{ + errno = *p_saved_errno; +} +#define NM_AUTO_PROTECT_ERRNO(errsv_saved) nm_auto(_nm_auto_protect_errno) _nm_unused const int errsv_saved = (errno) + /*****************************************************************************/ /* http://stackoverflow.com/a/11172679 */