mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-08 11:19:16 +02:00
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*.
(cherry picked from commit 312cea870d)
This commit is contained in:
parent
9afbaa86ce
commit
f39138ea48
1 changed files with 27 additions and 1 deletions
|
|
@ -22,7 +22,9 @@
|
||||||
#ifndef __NM_MACROS_INTERNAL_H__
|
#ifndef __NM_MACROS_INTERNAL_H__
|
||||||
#define __NM_MACROS_INTERNAL_H__
|
#define __NM_MACROS_INTERNAL_H__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "nm-glib.h"
|
#include "nm-glib.h"
|
||||||
|
|
||||||
|
|
@ -59,7 +61,31 @@ _nm_auto_free_gstring_impl (GString **str)
|
||||||
}
|
}
|
||||||
#define nm_auto_free_gstring nm_auto(_nm_auto_free_gstring_impl)
|
#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 */
|
/* http://stackoverflow.com/a/11172679 */
|
||||||
#define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway)
|
#define _NM_UTILS_MACRO_FIRST(...) __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, throwaway)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue