Skip to content

Commit fed8137

Browse files
committed
errno-util: add new errno_or_else() helper
1 parent 4747b64 commit fed8137

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/basic/errno-util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ static inline char *strerror_safe(int error) {
3636
return strerror(abs(error));
3737
}
3838

39+
static inline int errno_or_else(int fallback) {
40+
/* To be used when invoking library calls where errno handling is not defined clearly: we return
41+
* errno if it is set, and the specified error otherwise. The idea is that the caller initializes
42+
* errno to zero before doing an API call, and then uses this helper to retrieve a somewhat useful
43+
* error code */
44+
if (errno > 0)
45+
return -errno;
46+
47+
return -abs(fallback);
48+
}
49+
3950
/* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
4051
*
4152
* Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the

0 commit comments

Comments
 (0)